const char* is a pointer to a constant character char* const is a constant pointer to a character const char* const is a constant pointer to a constant character
I would like to point out that using int const * (or const int *) isn't about a pointer pointing to a const int variable, but that this variable is const for this specific pointer.
For example:
int var = 10;
int const * _p = &var;
The code above compiles perfectly fine. _p points to a const variable, although var itself isn't constant.
const * char is invalid C code and is meaningless. Perhaps you meant to ask the difference between a const char * and a char const *, or possibly the difference between a const char * and a char * const?
The const modifier is applied to the term immediately to its left. The only exception to this is when there is nothing to its left, then it applies to what is immediately on its right.
These are all equivalent ways of saying "constant pointer to a constant char":
const char*
is a pointer to a constant characterchar* const
is a constant pointer to a characterconst char* const
is a constant pointer to a constant characterI would like to point out that using
int const *
(orconst int *
) isn't about a pointer pointing to aconst int
variable, but that this variable isconst
for this specific pointer.For example:
The code above compiles perfectly fine.
_p
points to aconst
variable, althoughvar
itself isn't constant.const * char
is invalid C code and is meaningless. Perhaps you meant to ask the difference between aconst char *
and achar const *
, or possibly the difference between aconst char *
and achar * const
?See also:
The
const
modifier is applied to the term immediately to its left. The only exception to this is when there is nothing to its left, then it applies to what is immediately on its right.These are all equivalent ways of saying "constant pointer to a constant
char
":const char * const
const char const *
char const * const
char const const *
Another thumb rule is to check where const is: