What’s the difference between:
char * const
and
const char *
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The difference is that
const char *is a pointer to aconst char, whilechar * constis 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
constelement is the same as with a pointerconst.