const char * const versus const char *?
Solution 1:
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.
Solution 2:
-
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;