开发者

Difference between const char* p and char const* p [duplicate]

This question already has answers here: 开发者_开发知识库 Closed 12 years ago.

Possible Duplicate:

what is the difference between const int*, const int * const, int const *

    Are there any Difference between const char* p and char const* p 


const char* p is a pointer to a const char.

char const* p is a pointer to a char const.

Since const char and char const is the same, it's the same.

However, consider:

char * const p is a const pointer to a (non-const) char. I.e. you can change the actual char, but not the pointer pointing to it.


Some of the words are not in the same order.

(there's no semantic difference until the const moves relative to the star)


No difference, since the position of the '*' has not moved.

1) const char *p - Pointer to a Constant char ('p' isn't modifiable but the pointer is)
2) char const *p - Also pointer to a constant Char

However if you had something like:
char * const p - This declares 'p' to be a constant pointer to an char. (Char p is modifiable but the pointer isn't)


There is no functional difference between those two. The 'more precise' one is char const * p because the semantics are right to left.


There's no semantic difference, but it's a matter of coding style and readability. For complex expressions, reading from right to left works fine:

char const ** const

is a const pointer to a pointer to a constant char.

So char const * is more consistent in this regard. Many people, however, prefer const char* for its readibility - it is immediately clear what it means.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜