5

Possible Duplicate:
What is the difference between char * const and const char *?
const char * const versus const char *?

as we define a function in c,we may use (const char *str) , (char const *str) or (char *const str) as a variable.What's the difference among them?

Community
  • 1
  • 1
Alex
  • 207
  • 1
  • 2
  • 4
  • http://stackoverflow.com/q/4949254/274261 – ArjunShankar Oct 09 '12 at 14:28
  • pointer to const vs. const poointer... also check [What's the point of const pointers?](http://stackoverflow.com/questions/7715371/whats-the-point-of-const-pointers) – LihO Oct 09 '12 at 14:31
  • 2
    "Something like `const int * const * const x;` is likely overkill, even if it accurately describes how const x is" - from Google C++ Style Guide :) – Alex F Oct 09 '12 at 14:31
  • Follow the [spiral rule](http://c-faq.com/decl/spiral.anderson.html) – Mike Oct 09 '12 at 14:43
  • This one is not duplicated with the link marked as duplication, because it compares among 3, not just 2. – Eric Aug 11 '19 at 18:14

4 Answers4

10

The first two are equivalent, const char *str and char const *str both declare str to be a pointer to a char constant (that means that the char itself shouldn't be modified), the third one, char *const str declares str to be a constant pointer (meaning that once assigned it shouldn't be changed) to a char (which itself can be modified freely).

An interesting article regarding how to read type declarations is here, in case you want to check it.

lccarrasco
  • 2,031
  • 1
  • 17
  • 20
6

char const * str and const char * str are the same, as const applies to the term on its left or, if there isn't a type on the left side, to the right one. That's why you get a double const error on const char const *. Both are pointers on a constant char. You can change the value of the pointer, but not the dereferenced value:

const char * my_const_str;
my_const_str = "Hello world!"; // ok
my_const_str[0] = "h"; // error: my_const_str[0] is const!

char * const on the other hand is a constant pointer. You cannot change the pointer, but you can change the value dereferenced by the pointer.

char * const my_const_ptr = malloc(10*sizeof(char));
my_const_str[0] = "h"; // ok
my_const_str = "hello world"; // error, you would change the value of my_const_str!
Zeta
  • 103,620
  • 13
  • 194
  • 236
3

Read C declarations as:

Start at the variable. Look right, look left, then look right again (like crossing the road in the UK). When you see * you say "pointer to", when you see [] you say "array of", when to see () you say "function", etc. In your example cases there is nothing to the right;

const char *str "str is a pointer to a char which is constant"

char const *str "str is a pointer to a constant char" (same as above)

char *const str "str is a constant pointer to a char"

char ch = 'x';
const char cch = 'y';

const char *str = &cch;
char const *str = &cch;
char * const str = &ch;
cdarke
  • 42,728
  • 8
  • 80
  • 84
1
const char * str1: declare str as pointer to const char
char const * str2: declare str as pointer to const char
char * const str3: declare str as const pointer to char

So in the first two cases, the pointer is mutable, but the data referenced by the pointer is not.

In the last case, the pointer is not mutable, but the data inside is.

So, let's look at some operations:

str1[0];      // legal;
str1[0] += 3; // illegal;
str1 = NULL;  // legal;

str3[0];      // legal;
str3[0] += 3; // legal;
str3 = NULL;  // illegal;
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173