To avoid confusion, always append the const qualifier.
int * mutable_pointer_to_mutable_int;
int const * mutable_pointer_to_constant_int;
int *const constant_pointer_to_mutable_int;
int const *const constant_pointer_to_constant_int;
1) const char* x Here X is basically a character pointer which is pointing to a constant value
2) char* const x is refer to character pointer which is constant, but the location it is pointing can be change.
3) const char* const x is combination to 1 and 2, means it is a constant character pointer which is pointing to constant value.
4) const *char x will cause a compiler error. it can not be declared.
5) char const * x is equal to point 1.
the rule of thumb is if const is with var name then the pointer will be constant but the pointing location can be changed , else pointer will point to a constant location and pointer can point to another location but the pointing location content can not be change.
const always modifies the thing that comes before it (to the left of it), EXCEPT when it's the first thing in a type declaration, where it modifies the thing that comes after it (to the right of it).
So these two are the same:
int const *i1;
const int *i2;
they define pointers to a const int. You can change where i1 and i2 points, but you can't change the value they point at.
This:
int *const i3 = (int*) 0x12345678;
defines a const pointer to an integer and initializes it to point at memory location 12345678. You can change the int value at address 12345678, but you can't change the address that i3 points to.
Lots of answer provide specific techniques, rule of thumbs etc to understand this particular instance of variable declaration. But there is a generic technique of understand any declaration:
As per the clockwise/spiral rule a is pointer to character that is constant. Which means character is constant but the pointer can change. i.e. a = "other string"; is fine but a[2] = 'c'; will fail to compile
B)
char * const a;
As per the rule, a is const pointer to a character. i.e. You can do a[2] = 'c'; but you cannot do a = "other string";
Constant pointer: A constant pointer can point only to a single variable of the respective data type during the entire program.we can change the value of the variable pointed by the pointer. Initialization should be done during the time of declaration itself.
Syntax:
datatype *const var;
char *const comes under this case.
/*program to illustrate the behaviour of constant pointer */
#include<stdio.h>
int main(){
int a=10;
int *const ptr=&a;
*ptr=100;/* we can change the value of object but we cannot point it to another variable.suppose another variable int b=20; and ptr=&b; gives you error*/
printf("%d",*ptr);
return 0;
}
Pointer to a const value: In this a pointer can point any number of variables of the respective type but we cannot change the value of the object pointed by the pointer at that specific time.
Syntax:
const datatype *varor datatype const *var
const char* comes under this case.
/* program to illustrate the behavior of pointer to a constant*/
#include<stdio.h>
int main(){
int a=10,b=20;
int const *ptr=&a;
printf("%d\n",*ptr);
/* *ptr=100 is not possible i.e we cannot change the value of the object pointed by the pointer*/
ptr=&b;
printf("%d",*ptr);
/*we can point it to another object*/
return 0;
}
Two rules
If const is between char and *, it will affect the left one.
If const is not between char and *, it will affect the nearest one.
e.g.
char const *. This is a pointer points to a constant char.
char * const. This is a constant pointer points to a char.
To avoid confusion, always append the const qualifier.
1) const char* x Here X is basically a character pointer which is pointing to a constant value
2) char* const x is refer to character pointer which is constant, but the location it is pointing can be change.
3) const char* const x is combination to 1 and 2, means it is a constant character pointer which is pointing to constant value.
4) const *char x will cause a compiler error. it can not be declared.
5) char const * x is equal to point 1.
the rule of thumb is if const is with var name then the pointer will be constant but the pointing location can be changed , else pointer will point to a constant location and pointer can point to another location but the pointing location content can not be change.
const
always modifies the thing that comes before it (to the left of it), EXCEPT when it's the first thing in a type declaration, where it modifies the thing that comes after it (to the right of it).So these two are the same:
they define pointers to a
const int
. You can change wherei1
andi2
points, but you can't change the value they point at.This:
defines a
const
pointer to an integer and initializes it to point at memory location 12345678. You can change theint
value at address 12345678, but you can't change the address thati3
points to.Lots of answer provide specific techniques, rule of thumbs etc to understand this particular instance of variable declaration. But there is a generic technique of understand any declaration:
A)
As per the clockwise/spiral rule
a
is pointer to character that is constant. Which means character is constant but the pointer can change. i.e.a = "other string";
is fine buta[2] = 'c';
will fail to compileB)
As per the rule,
a
is const pointer to a character. i.e. You can doa[2] = 'c';
but you cannot doa = "other string";
Syntax:
char *const
comes under this case.Syntax:
const datatype *var
ordatatype const *var
const char*
comes under this case.