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).
const int * And int const * are the same. const int * const And int const * const are the same. If you ever face confusion in reading such symbols, remember the Spiral rule: Start from the name of the variable and move clockwise to the next pointer or type. Repeat until expression ends.
char* p and char *p are exactly equivalent. In many ways, you ought to write char *p since, really, p is a pointer to char . But as the years have ticked by, most folk regard char* as the type for p , so char* p is possibly more common.
In the constant pointers to constants, the data pointed to by the pointer is constant and cannot be changed. The pointer itself is constant and cannot change and point somewhere else.
The latter prevents you from modifying the_string
inside print_string
. It would actually be appropriate here, but perhaps the verbosity put off the developer.
char* the_string
: I can change which char
the_string
points to, and I can modify the char
to which it points.
const char* the_string
: I can change which char
the_string
points to, but I cannot modify the char
to which it points.
char* const the_string
: I cannot change which char
the_string
points to, but I can modify the char
to which it points.
const char* const the_string
: I cannot change which char
the_string
points to, nor can I modify the char
to which it points.
Mutable pointer to a mutable character
char *p;
Mutable pointer to a constant character
const char *p;
Constant pointer to a mutable character
char * const p;
Constant pointer to a constant character
const char * const p;
const char * const
means pointer as well as the data the pointer pointed to, are both const!
const char *
means only the data the pointer pointed to, is const. pointer itself however is not const.
Example.
const char *p = "Nawaz";
p[2] = 'S'; //error, changing the const data!
p="Sarfaraz"; //okay, changing the non-const pointer.
const char * const p = "Nawaz";
p[2] = 'S'; //error, changing the const data!
p="Sarfaraz"; //error, changing the const pointer.
(I know this is old but I wanted to share anyway.)
Just wanted to elaborate on Thomas Matthews' answer. The Right-Left Rule of C type declarations pretty much says: when reading a C type declaration start at the identifier and go right when you can and left when you can't.
This is best explained with a couple examples:
Start at the identifier, we can't go right so we go left
const char* const foo
^^^^^
foo is a constant...
Continue left
const char* const foo
^
foo is a constant pointer to...
Continue left
const char* const foo
^^^^
foo is a constant pointer to char...
Continue left
const char* const foo
^^^^^
foo is a constant pointer to char constant (Complete!)
Start at the identifier, we can't go right so we go left
char* const foo
^^^^^
foo is a constant...
Continue left
char* const foo
^
foo is a constant pointer to...
Continue left
char* const foo
^^^^
foo is a constant pointer to char (Complete!)
Start at the identifier, but now we can go right!
const char* const* (*foo[8])()
^^^
foo is an array of 8...
Hit parenthesis so can't go right anymore, go left
const char* const* (*foo[8])()
^
foo is an array of 8 pointer to...
Finished inside parenthesis, can now go right
const char* const* (*foo[8])()
^^
foo is an array of 8 pointer to function that returns...
Nothing more to the right, go left
const char* const* (*foo[8])()
^
foo is an array of 8 pointer to function that returns a pointer to a...
Continue left
const char* const* (*foo[8])()
^^^^^
foo is an array of 8 pointer to functions that returns a pointer to a constant...
Continue left
const char* const* (*foo[8])()
^
foo is an array of 8 pointer to functions that returns a pointer to a constant pointer to a...
Continue left
const char* const* (*foo[8])()
^^^^
foo is an array of 8 pointer to functions that returns a pointer to a constant pointer to a char...
Continue left
const char* const* (*foo[8])()
^^^^^
foo is an array of 8 pointer to functions that returns a pointer to a constant pointer to a char constant (Complete!)
Further explanation: http://www.unixwiz.net/techtips/reading-cdecl.html
Many people suggest reading the type specifier from right to left.
const char * // Pointer to a `char` that is constant, it can't be changed.
const char * const // A const pointer to const data.
In both forms, the pointer is pointing to constant or read-only data.
In the second form, the pointer cannot be changed; the pointer will always point to the same place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With