What's the difference among (const char *str) , (char const *str) and (char *const str)?
Solution 1
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.
Solution 2
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!
Solution 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;
Solution 4
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;
Alex
Updated on June 17, 2022Comments
-
Alex almost 2 years
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?
-
Mawg says reinstate Monica over 3 yearsSo, how to double const, so that you can change neither?
-
Zeta over 3 years@MawgsaysreinstateMonica that's a new question and should get posted as one ;), although it's already answered here: stackoverflow.com/q/1143262/1139697. (
const char * const str
, btw).