typedef char* c;
const c ptr1 = "pointer";
++ptr1; /// error
const char* ptr2 = "pointer";
++ptr2; /// runs fine
Now ptr1 should be of type const char* and thus a non-const pointer, then why is it being treated as a constant pointer ?
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.
They are not the same.
The first designates a const-pointer-to-char, the second is a pointer-to-const-char.
Try reading right to left:
By using the typedef
typedef char* cyou pack the meaning “pointer to char” into a single aliasc:Additional explanations:
Typedefs are not in-place-expanded like macros are, i.e.
really becomes
it does not become
Don’t expand it like macros in your mind, with the typedefs, you’ve bound
charand*together and formed an atom.