Found 7401 Articles for C++

What should we assign to a C++ pointer: A Null or 0?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

955 Views

In C++, Null is defined as 0. Null or 0 is an integer.In case of a pointer, we can assign a pointer p as −Float* p = NULL; Float* p = 0; Float* p = nullptr;3 of them will produce the same result. null ptr is a keyword introduced in C++11 as a replacement to NULL.

What is the size of a pointer in C/C++?

Samual Sam
Updated on 30-Jul-2019 22:30:25

8K+ Views

The size of a pointer in C/C++ is not fixed. It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed.It is common to all data types like int *, float * etc.

void pointer in C

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

10K+ Views

The void pointer in C is a pointer which is not associated with any data types. It points to some data location in the storage means points to the address of variables. It is also called general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.It has some limitations −1) Pointer arithmetic is not possible with void pointer due to its concrete size.2) It can’t be used as dereferenced.AlgorithmBegin    Declare a of the integer datatype.       Initialize a = 7.    Declare b of the float datatype.       Initialize b ... Read More

NULL pointer in C

Samual Sam
Updated on 30-Jul-2019 22:30:25

6K+ Views

A null pointer is a pointer which points nothing.Some uses of the null pointer are:a) To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet.b) To pass a null pointer to a function argument when we don’t want to pass any valid memory address.c) To check for null pointer before accessing any pointer variable. So that, we can perform error handling in pointer related code e.g. dereference pointer variable only if it’s not NULL.AlgorithmBegin.    Declare a pointer p of the integer datatype.       Initialize *p= NULL.    Print “The value of ... Read More

Double Pointer (Pointer to Pointer) in C

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

10K+ Views

A pointer is used to store the address of variables. So, when we define a pointer to pointer, the first pointer is used to store the address of the second pointer. Thus it is known as double pointers.AlgorithmBegin    Declare v of the integer datatype.       Initialize v = 76.    Declare a pointer p1 of the integer datatype.    Declare another double pointer p2 of the integer datatype.    Initialize p1 as the pointer to variable v.    Initialize p2 as the pointer to variable p1.    Print “Value of v”.       Print the value ... Read More

What is the easiest way to initialize a std::vector with hardcoded elements in C++?

Samual Sam
Updated on 30-Jul-2019 22:30:25

482 Views

In modern C++ [11,14,…] a vector is initialized in the following waystd::vector vec = {1,2,3};AlgorithmBegin    Initialize the vector v.    Using accumulate, sum up all the elements of the vector v is done.    Print the result. End.Here is a simple example of sum up the elements of a vector:Example Live Demo#include #include #include using namespace std; int main() {    vector v = {2,7,6,10};    cout

Ways to copy a vector in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

9K+ Views

There are different ways to copy a vector in C++.1) std::copystd:: copy is inbuilt to copy the elements from one vector to another.Syntaxstd::copy(first_iterator_o, last_iterator_o, back_inserter()): first_iteratot_0 = First iterator of first vector. last_iteratot_0 = Last iterator of first vector. back_inserter() = To insert values from back.AlgorithmBegin    Declare v1 of vector type.       Initialize some values into v1 vector in array pattern.    Declare v2 of vector type.    Call copy(v1.begin(), v1.end(), back_inserter(v2)) to copy all    elements of v1 to v2.    Print “v1 vector elements are :”.    for (int i=0;iRead More

Sorting a vector in descending order in C++

Samual Sam
Updated on 30-Jul-2019 22:30:25

16K+ Views

Sorting a vector in C++ can be done by using std::sort(). It is defined in header. To get a stable sort std::stable_sort is used. It is exactly like sort() but maintain the relative order of equal elements. Quicksort(), mergesort() can also be used, as per requirement.Sorting a vector in descending order can be done by using std::greater ().AlgorithmBegin    Declare v of vector type.       Initialize some values into v in array pattern.    Print “Elements before sorting”.    for (const auto &i: v)       print all the values of variable i.    Print “Elements after ... Read More

Sorting a vector in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

15K+ Views

Sorting a vector in C++ can be done by using std::sort(). It is defined in header. To get a stable sort std::stable_sort is used. It is exactly like sort() but maintains the relative order of equal elements. Quicksort(), mergesort() can also be used, as per requirement.AlgorithmBegin    Decalre v of vector type.       Initialize some values into v in array pattern.    Print “Elements before sorting”.    for (const auto &i: v)       print all the values of variable i.    Print “Elements after sorting”.    Call sort(v.begin(), v.end()) function to sort all the elements of ... Read More

Passing a vector to constructor in C++

Samual Sam
Updated on 30-Jul-2019 22:30:25

989 Views

This is a simple C++ program to pass a vector to a constructor.AlgorithmBegin    Declare a class named as vector.       Declare vec of vector type.       Declare a constructor of vector class.          Pass a vector object v as a parameter to the constructor.          Initialize vec = v.          Declare a function show() to display the values of vector.             for (int i = 0; i < vec.size(); i++)                print the all values ... Read More

Advertisements