/*const char * p;
char * const p;
const char * const p;*/ // these are the three conditions,
// const char *p;const char * const p; pointer value cannot be changed
// char * const p; pointer address cannot be changed
// const char * const p; both cannot be changed.
#include<stdio.h>
/*int main()
{
const char * p; // value cannot be changed
char z;
//*p = 'c'; // this will not work
p = &z;
printf(" %c\n",*p);
return 0;
}*/
/*int main()
{
char * const p; // address cannot be changed
char z;
*p = 'c';
//p = &z; // this will not work
printf(" %c\n",*p);
return 0;
}*/
/*int main()
{
const char * const p; // both address and value cannot be changed
char z;
*p = 'c'; // this will not work
p = &z; // this will not work
printf(" %c\n",*p);
return 0;
}*/
The difference is that const char * is a pointer to a const char, while char * const is a constant pointer to a char.
The first, the value being pointed to can't be changed but the pointer can be. The second, the value being pointed at can change but the pointer can't (similar to a reference).
There is also a
const char * const
which is a constant pointer to a constant char (so nothing about it can be changed).
Note:
The following two forms are equivalent:
const char *
and
char const *
The exact reason for this is described in the C++ standard, but it's important to note and avoid the confusion. I know several coding standards that prefer:
char const
over
const char
(with or without pointer) so that the placement of the const element is the same as with a pointer const.
Rule of thumb: read the definition from right to left!
const int *foo;
Means "foo points (*) to an int that cannot change (const)".
To the programmer this means "I will not change the value of what foo points to".
*foo = 123; or foo[0] = 123; would be invalid.
foo = &bar; is allowed.
int *const foo;
Means "foo cannot change (const) and points (*) to an int".
To the programmer this means "I will not change the memory address that foo refers to".
*foo = 123; or foo[0] = 123; is allowed.
foo = &bar; would be invalid.
const int *const foo;
Means "foo cannot change (const) and points (*) to an int that cannot change (const)".
To the programmer this means "I will not change the value of what foo points to, nor will I change the address that foo refers to".
I presume you mean const char * and char * const .
The first, const char *, is a pointer to a constant character. The pointer itself is mutable.
The second, char * const is a constant pointer to a character. The pointer cannot change, the character it points to can.
And then there is const char * const where the pointer and character cannot change.
char * const and const char *?
const char * p;
// value cannot be changedchar * const p;
// address cannot be changedconst char * const p;
// both cannot be changed.Here is a detailed explanation with code
First one is a syntax error. Maybe you meant the difference between
and
In that case, the first one is a pointer to data that can't change, and the second one is a pointer that will always point to the same address.
The difference is that
const char *
is a pointer to aconst char
, whilechar * const
is a constant pointer to achar
.The first, the value being pointed to can't be changed but the pointer can be. The second, the value being pointed at can change but the pointer can't (similar to a reference).
There is also a
which is a constant pointer to a constant char (so nothing about it can be changed).
Note:
The following two forms are equivalent:
and
The exact reason for this is described in the C++ standard, but it's important to note and avoid the confusion. I know several coding standards that prefer:
over
(with or without pointer) so that the placement of the
const
element is the same as with a pointerconst
.Rule of thumb: read the definition from right to left!
const int *foo;
Means "
foo
points (*
) to anint
that cannot change (const
)".To the programmer this means "I will not change the value of what
foo
points to".*foo = 123;
orfoo[0] = 123;
would be invalid.foo = &bar;
is allowed.int *const foo;
Means "
foo
cannot change (const
) and points (*
) to anint
".To the programmer this means "I will not change the memory address that
foo
refers to".*foo = 123;
orfoo[0] = 123;
is allowed.foo = &bar;
would be invalid.const int *const foo;
Means "
foo
cannot change (const
) and points (*
) to anint
that cannot change (const
)".To the programmer this means "I will not change the value of what
foo
points to, nor will I change the address thatfoo
refers to".*foo = 123;
orfoo[0] = 123;
would be invalid.foo = &bar;
would be invalid.