typedef char* ptr;
const ptr p;
Which is true:
p points to a constant character; orp is a constant and points to a character. Please explain the reason
typedef char* ptr;
const ptr p;
The latter line is equivalent to
char * const p;
i.e. p is a const pointer to char. The typedef introduces a new name for a type, it is not a textual substitution.
First, let's take the typedef out of the equation for a moment.
const char *p and char const *p both declare p as a non-const pointer to const data; you can assign p to point to different things, but you cannot modify the thing being pointed to.
char * const p declares p as a const pointer to non-const data; you cannot change p to point to a different object, but you can modify the thing p is pointing to.
const char * const p and char const * const p both declare p as a const pointer to const data. That should be fairly self-explanatory.
The typedef is a little non-intuitive. ptr is a synonym for char *, so const ptr acts as char * const; the const qualifier is being applied to the pointer type, not the char type.
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