The difference between const char * p and char const * p

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 
+6
c
source share
5 answers

Some words are not in the same order.

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

+11
source share

const char* p is a pointer to const char .

char const* p is a pointer to char const .

Since const char and char const one and the same, then the same.

However, consider:

char * const p is a const pointer for a (not const) char. That is, you can change the actual char, but not the pointer pointing to it.

+17
source share

It makes no difference because the position '*' did not move.

1) const char * p - pointer to constant char ('p' cannot be changed, but there is a pointer)
2) char const * p - also a pointer to char constant

However, if you have something like:
char * const p - declares 'p' a constant pointer to char. (Char p is modified, but the pointer is not)

+4
source share

There are no differences between the two functional differences. "More precise" is char const * p , because the semantics are from right to left.

+2
source share

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

char const ** const

is const pointer to a pointer to a constant char .

So char const * is more consistent in this regard. However, many people prefer const char* for their readability - it immediately becomes clear what this means.

+2
source share

All Articles