简体   繁体   中英

Pointers,constants and Type aliasis

What is the difference between the following:

const char *c = 0; //Here c is a pointer to a constant character

typedef char *pstring;
const pstring cstr = 0 //cstr is a constant pointer to a character

Why is there a difference between the two statements, while they look just the same.In C++ Primer it is mentioned that the base type of first statement is const char and * is the part of the declarator. While for the last statement,the base type is const pstring. I am not getting the difference between the two.

I think your confusion stems from not knowing about top-level const . When you have a typedef , you cannot add const to the underlying type, only above it.

const pstring p is not the same as const char *p ie typedef s are not macro substitutions. Once typedef ed the type is atomic; it doesn't matter where the const is, be it to the left or right of the type the const ness is associated with the complete type, in this case char* .

typedef char* pstring;
const pstring p1;  // const pointer to char i.e. char* const
pstring const p2;  // const pointer to char i.e. char* const

If you have the original type written as it is then the meaning will change.

const char* p1;  // pointer to const char
char* const p2;  // const pointer to char

What are top-level const qualifiers? Read to know more.

Read pointer declarations from right to left helps to make sense of them.

First one is a pointer to a char that is a const. You can't modify the char that it points to, because the char is const.

Second one is a const pointer to a char. You can't modify the value of the pointer (the location of the char it points to), because the pointer is const.

Following a typedef char* pstring (and it's a good idea to move the asterisk away from your typename, as it isn't part of it), the constness applies to the type that it creates, as it does with everything else:

int i;
const int i = 0; // actually int const i = 0

char* p;
const char* p = 0; // actually char const* p = 0

typedef char* pstring;
const pstring ps = 0; // actually 'pstring const' -> 'char* const' ps = 0

Exactly for this reason, some coding standards (eg MISRA) require you to place the cv qualifiers after the type name, because it works better to show that the const-ness qualifies the type, it's dependent on it - and not the other way around.

const char *c = 0;

This declares c to be a pointer to const char . You cannot change what c points to through c . However c can point to a different string.

char arr[] = "test";
c = arr; // OK

On other other hand.

typedef char *pstring;
const pstring cstr = 0;

declares cstr to be const pointer to char . You can change what cstr points to but you cannot change where cstr points to. Without the typedef , you would use:

char* const cstr = 0;

Unfortunately, the language allows const to be place before the fundamental type. It will be much clearer to use:

char const* c = 0;     // what c points to is const

Read from right to left. c is a pointer to const char .

char* const cstr = 0;  // the pointer is const

Read from right to left. c is a const pointer to char .

IMO, the const -ness of the pointer and the object it points is clearer with the above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM