C++ pointers – everything you need to know about

In C++ a pointer is a variable that can point to another variable or object.When we say pointer we are referring to a variable that can hold the address of another variable.And note a pointer can hold only address value not any other value,not even constant or literals.To make variable a pointer we simply add the sign ‘*‘ in front of the variable name.

Link what is Literals?

int i, //'i' is variable
 *ip ; //ip is a pointer of int type 

To make a pointer point to another variable we must assign the variable address to the pointer.We can do this by assigning the variable reference to it.

int i=90 ,
*ip=&i ; //Assigns ‘i’ address to ip

Suppose if the memory in which the value 90 or the variable ‘i’ is stored is 0X26ab then the picture below shows what the content of ‘ip’ look like.

Accessing pointers

To access the value a pointer points to we can use the pointer name with ‘*’ sign.You must always include the sign,without it you will get the address value of the variable which it points to;the pointer stores that address value.

int i=90 , *ip=&i ;

cout<< *ip << ” ” << i << endl ;

cout<< ip << ” ” << &i << endl ; 


90    90
0X26ab    0X26ab

Here you can see ‘*ip’ gives the value of the variable ‘i’ but ‘ip’ gives the address of the variable ‘i’.

Assigning new value to a pointer

A pointer cannot be initialized with a literal directly,if you do so you will get an error.

 int *ip=90 ; //Error!! direct initialization not allowed 

But however after you have made the pointer point to another variable you can assign a literal to the pointer directly.By doing so it changes the value of the variable it points to.This is due to the fact that since a pointer points to the variable address so when assigning a new value we are actually changing the value in the memory pointed by that pointer.

string st=”New string” , *sPt=&st ;

cout<< *st << endl ;

*sPt=”Happy and sad”;

cout<< st << endl
<< *sPt << endl;

New string
Happy and sad
Happy and sad

You can see that ‘st’ also gives the new string value.

Pointer can be made to point to another variable

Unlike the reference which cannot be made to refer to another variable once it has referred to certain variable,a pointer which points to a variable can be made to point to another variable but of the same type.If you try to make it point to a variable of different type the compiler will complain error.

Link what is Reference?

int i=90 , *ip=&i ;
int ii=546 ;
*ip=&ii ; //works fine,same type

float f ;
*ip=&f ; //compiler will complain here,different type 

NULL pointer

A pointer can be ‘null pointer‘ meaning it points to nothing.To make a pointer point to nothing a pointer must be initialized to 0 or nullptr or NULL(C type).

Note assigning pointer to 0 or NULL is a C way of making pointer a null pointer and at times this method is insecure.You must prefer the new value:nullptr and use it whenever you want to make a pointer a null pointer.

int *inull=nullptr ; //preferred method
int *inull=0 ; //works fine
/*or */
int *inull=NULL ; //works fine but C way not recommended

why should we want to make a pointer a null pointer?

Sometimes under some circumstances we have no more used for a pointer.In such case it is better to make the pointer a null pointer.By making it a null pointer we are letting others(whoever takes interest in that pointer again) know that the pointer points to nothing.If it is not made a null pointer who knows,someone else might try to use that pointer and this time accessing that pointer will give some undefined value.And I assure you,an undefined value can give rise to a huge problem in your program.

int *pt ;

 int ival=12345 ;
 pt=&ival ;
 cout<< *pt << endl ;

cout<< *pt ; //gives some random undefined value 

If yuo run the program the lat ‘cout’ will give you undefine value.Since ‘ival’ has a local scope,outside it’s scope ‘ival’ has ceased to exist.Outside this region if you access it you get an undefined value.But if we had made it a null pointer we would have gotten a value 0 or the compiler would provide some other sign to signal us that the pointer is a null pointer.Instead we got a random value which is confusing and unexpected.

Link Local scope and global scope

*Note::It is always a good practice to initialize a pointer to null value or some other value when it is declared because most of the error in our program originates from non-initialized pointer and there is no way of differentiating an initialized and non-initialized pointer while debugging.

Incrementing a pointer

We know a pointer with the ‘*’ sign refers to the value of the variable which the pointer points to,so if we increment a pointer with the ‘*’ sign then the value of the variable which it points to is increased.But if we increment the pointer without the ‘*’ sign then the address of the variable it points to is increased.Look at the code below.

int i4=9 , *i4p=&i4 ;

++(*i4p) ;
cout<< i4 << ” ” << *i4p << endl ;

cout<< (int)&i4 << ” ” << (int)i4p << endl ; 

The outputs are,

10    10
6946516    6946520

In the first output the i4 variable value is incremented by 1.

In the second output the address value of the pointer is incremented by 4 bytes from the original address value which it points to;the address of i4 variable.The address value is increased by 4 bytes because the pointer points to int type whose size is 4 bytes,if the pointer were of double type then the address value would have increased by 8 bytes because the size of double type is 8 bytes.

double d=9.0 , *dp=&d ;
++dp ;

cout<< &d << ” ” << (int)dp << endl ;   /* the difference in address value is 8 bytes */

char c=’6′ , *cp=&c ;
++cp ;
cout<< (int)&c << ” ” << (int)cp << endl ;   /* The difference in address value is 2 bytes */

size of a pointer

The size of a pointer depends on the compiler and the machine.In my machine which is a 64 bits system.I found some interesting results.They are shown below.

When the compiler is 64 bits and platform is also 64 bits.The size of a pointer is 8 bytes no matter what the type is,so running the code below.
int *i9=nullptr ;
double *df=nullptr ;

cout<< sizeof(i9) << endl
<< sizeof(df) ;

The output is,


The size of all the pointers is 8 bytes.

When the compiler is 32 bits running in 64 bits platform.The size of all the data type pointers is 4 bytes.

This one is tested using Visual Studio 2015.

char *cpt=nullptr;
long double *ld=nullptr;

cout<< sizeof(cpt) << endl
<< sizeof(ld) ;



You can try for different types also.

**Note::Do check the size of pointers of different types in your machine and compiler.Comment below if you find anything different.