tags:

views:

554

answers:

8
+4  Q: 

Const Char

What's the difference between:

char * const and const char * ?

UPDATE: char * const instead of const * char

+5  A: 

I'm pretty sure that const * char isn't going to compile, at least not in C...

ahockley
const = 5; char = 4; if ((const * char) == 20) puts("20"); //;->
jrcs3
Shouldn't this be a comment. Not an answer?
blak3r
+2  A: 

const char* is a pointer to a constant character
char* const is a constant pointer to a character
const char* const is a constant pointer to a constant character

Andrew Coleson
+1  A: 

I presume you mean const char * and char * const .

The first, const char *, is a pointer to a constant character. The pointer itself is mutable.

The second, char * const is a constant pointer to a character. The pointer cannot change, the character it points to can.

And then there is const char * const where the pointer and character cannot change.

Michael
Your first two are actually the same and your third is a compiler error :)
workmad3
Whoops, you are right. Fixed.
Michael
+16  A: 

If you meant the difference between

const char *

and

char * const

then the difference is that const char * is a pointer to a const char, while char * const is a constant pointer to a char.

The first, the value being pointed to can't be changed but the pointer can be. The second, the value being pointed at can change but the pointer can't (similar to a reference).

There is also a

const char * const

which is a constant pointer to a constant char (so nothing about it can be changed).

Note:

The following two forms are equivalent:

const char *

and

char const *

The exact reason for this is described in the C++ standard, but it's important to note and avoid the confusion. I know several coding standards that prefer:

char const

over

const char

(with or without pointer) so that the placement of the const element is the same as with a pointer const.

workmad3
Hey workmad3 :)
GMan
Howdy GMan :) didn't realise you were on here :P
workmad3
+1  A: 

First one is a syntax error. Maybe you meant the difference between

const char * mychar

and

char * const mychar

In that case, the first one is a pointer to data that can't change, and the second one is a pointer that will always point to the same address.

Javier Badia
+11  A: 

const * char is invalid C code and is meaningless. Perhaps you meant to ask the difference between a const char * and a char const *, or possibly the difference between a const char * and a char * const?

See also:

Adam Rosenfield
++for referencing existing questions. I added another...
Shog9
+1  A: 

const always modifies the thing that comes before it (to the left of it), EXCEPT when it's the first thing in a type declaration, where it modifies the thing that comes after it (to the right of it).

So these two are the same:

int const *i1;
const int *i2;

they define pointers to a const int. You can change where i1 and i2 points, but you can't change the value they point at.

This:

int *const i3 = (int*) 0x12345678;

defines a const pointer to an integer and initializes it to point at memory location 12345678. You can change the int value at address 12345678, but you can't change the address that i3 points to.

Don McCaughey
+2  A: 

To avoid confusion, always append the const qualifier.

int       *      mutable_pointer_to_mutable_int;
int const *      mutable_pointer_to_constant_int;
int       *const constant_pointer_to_mutable_int;
int const *const constant_pointer_to_constant_int;
You can call me Chuck