A pointer is a variable that can point to another variable or object.So 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 an 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.
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.
*ip=&i ; ///Assigns ‘i’ address to ip
Suppose if the memory in which the value 90 or the variable is stored is 0X26ab then the picture below shows what the content of ‘ip’ look like.
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;actually it stores that address value.
cout<< *ip << ” ” << i << endl ;
cout<< ip << ” ” << &i << endl ;
Assigning new value to a pointer
A pointer cannot be initialized to a literal value directly and if you do so you will get an error.
But however after you have made the pointer point to another variable you can assign a literals to the pointer directly.By doing so it changes the value of the variable also.This is due to the fact that since a pointer points to the variable address 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;
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 : reference
int ii=546 ;
*ip=&ii ; ///works fine,same type
float f ;
*ip=&f ; ///compiler will complain here
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=0 ; ///works fine
int *inull=NULL ; ///works fine but C type not recommended
why should we make a pointer a null pointer?
Sometimes under some circumstances we have no more used for the 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 asure you an undefined value can give rise to a huge problem in your program.
int ival=12345 ;
cout<< *pt << endl ;
cout<< *pt ; //gives some random undefined value
Since the ival has a local scope,outside it’s scope the 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.
*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 name with the ‘*’ sign refers to the value of the variable which the pointer points to so, if we increment a pointer including 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.
cout<< i4 << ” ” << *i4p << endl ;
cout<< (int)&i4 << ” ” << (int)i4p << endl ;
The outputs are,
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 increased by 8 bytes because the size of double type is 8 bytes.
cout<< &d << ” ” << (int)dp << endl ; /* the difference in address value is 8 bytes */
char c=’6′ , *cp=&c ;
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 you are running your program in.In my machine which is a 64 bits system.I found some interesting results.They are shown below.
When the compiler is 64 bits.The size of a pointer is 8 bytes no matter what the type is,so running the code below.
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 in 64 bits platform.The size of all the data type pointers is 4 bytes.
This one is tested using Visual Studio 2015.
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.
Stop wasting time,earn money($$$) from your website-Join Now!