If you remember the "const" keyword before a variable name implies that the variable is a constant and can not be modified during the runtime. This saves us from accidental manipulation of a value. For example days of the week are 7 and will remain like this in centuries to come. If we declare the variable days as "int days", a statement like "days++" can accidently increment to an invalid value. Therefore, we should declare the variable as "int const days=7", this will make the variable as constant. If a statment is written to change the value, the compiler will show an error specifiying that the variable can not be modified since it is a constant.
Similarly if a pointer is declared as a constant, we can not change it to point to any other value. This means a constant pointer always points to a fixed value. Let's see how it works
Similarly if a pointer is declared as a constant, we can not change it to point to any other value. This means a constant pointer always points to a fixed value. Let's see how it works
int x=10;
int *ptr=&x;
int * const cptr=&x;
++ *ptr; // Valid , increments the value at ptr
++ *cptr; // Valid, increments the value at cptr
++ cptr;// Invalid, since cptr is a constant pointer and can not be modified.
In the above code, x is an integer variable with a value of 10. ptr is a pointer to x so is cptr. The difference between the two pointers is that ptr can be modified to point to any other integer while cptr is a constant pointer. When the statement "++cptr" is encountered the compiler will return an error stating that the pointer can not be modified.
Pointer to a constant
A pointer which points to a constant is called "Pointer to a constant". Using a "pointer to a constant", the constant value can not be modified but the pointer can be made to point to another location. Consider the following code :
const int days=7; // a constant integer variable
const int *ptr=&days; // a pointer to a constant
++ *ptr; // Invalid, since the statement will try to increment the value of days which is a constant
++ ptr; // Valid. Since the pointer is not a constant. The value to which it is pointing is.
This is a simple declaration of the variable myPtr. myPtr is a pointer to a character variable and in this case points to the character 'A'. Don't be confused about the fact that a character pointer is being used to point to a single character—this is perfectly legal! Not every character pointer has to point to a string.
Now consider the following three declarations assuming that char_A has been defined as a type char variable.:
const char * myPtr = &char_A;
char * const myPtr = &char_A;
const char * const myPtr = &char_A;
They are all three valid and correct declarations. Each assigns the addres of char_A to a character pointer. The difference is in what is constant.
The first declaration:
const char * myPtr
declares a pointer to a constant character. You cannot use this pointer to change the value being pointed to:
char char_A = 'A';
const char * myPtr = &char_A;
*myPtr = 'J'; // error - can't change value of *myPtr
The second declaration,
char * const myPtr
declares a constant pointer to a character. The location stored in the pointer cannot change. You cannot change where this pointer points:
char char_A = 'A';
char char_B = 'B';
char * const myPtr = &char_A;
myPtr = &char_B; // error - can't change address of myPtr
The third declares a pointer to a character where both the pointer value and the value being pointed at will not change.