What is the difference between char * const and const char *?
c, constants, pointers
Solution
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`.
Problem
What's the difference between: ``` char * const ``` and ``` const char * ```
Related problems
- Why are both "const T" and "T const" valid, and which one should you use?
- What is the difference between const int*, const int * const, and int const *?
- Why can I change the value of a const char* variable?
- 'const int' vs. 'int const' as function parameters in C++ and C
- Difference between const declarations in C++
- What use are const pointers (as opposed to pointers to const objects)?
- C++ const question