Tuesday, January 24, 2012

What is the difference betweein pointer to constant or a constant pointer?

Lets Take an example :
We have :

char *const p1="Shashwat";
char const* p2="Shriparv";



p2 is a pointer to a constant "Shriparv"
p2 can change to point to another constant string, but you can't change the value "Shriparv" thru p2.

p1 is a constant pointer to "Shashwat"
p1 can't change, but you can change "Shashwat" thru p1. 



char const* p2 = "Shriparv";
*p2 = 'Somenew Shriparv Pointer'; // not allowed.
p2 = "New Shriparv";  // allowed

char * const p1 = "Shashwat";
*p1 = 'C'; // allowed
p1 = "New Shashwat"; // not allowed

No comments:

Post a Comment

Live

Your Ad Here