8

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 
Community
  • 1
  • 1
  • The dupe is here: http://stackoverflow.com/q/890535/694576 not where it had been close for, as the latter is about C++. – alk Sep 26 '15 at 11:17

5 Answers5

20

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.

Aleksandra Zalcman
  • 3,102
  • 1
  • 17
  • 19
phimuemue
  • 32,638
  • 9
  • 79
  • 112
14

Some of the words are not in the same order.

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

Pete Kirkham
  • 47,852
  • 5
  • 89
  • 165
4

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)

IntelliChick
  • 626
  • 4
  • 7
2

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

Amardeep AC9MF
  • 17,715
  • 5
  • 39
  • 50
2

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.

Alexander Gessler
  • 44,353
  • 6
  • 80
  • 121