View Single Post
  #3 (permalink)  
Old June 5th, 2007, 01:39 AM
rick84 rick84 is offline
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Constant Pointer: It means that the address that the pointer is pointing to cannnot be changed. Consider this
char str[] = "Hello";
char * const ptr = &str;
In the above two statements first we have created an array of char called str then in the next statement we have created a char pointer ptr which points to str. Since prt is a constant pointer it can only point to str. i.e if we write something like ptr = &str1. the compiler will flag an error because we cannot change the contents of pointer ptr though we can change the contents of string str;

Pointer to Constant: It simply means that the string is constant and the pointer is not constant. Now we can change the contents of pointer but not that of string through the use of pointer.
char str[] = "Hello";
const char * ptr = &str;
char str1[] = "World"
*(ptr+2) = 't' //error constant pointer cannot be used to modify string
ptr = &str1 //ok

Reply With Quote