16

First of all, what's the difference between:

(1) const char*
(2) char const*
(3) const char const*

I'm fairly certain I understand this fully, but I'd like someone to give me a sentence for each, specifically, so it sticks in my head. It's one of those things that I'm fine with until someone puts me on the spot and then it gets fuzzy!

Also, how are string-literalls stored by the compiler? This isn't homework, I'm just brushing up on C for interviews in case anyone cares.

asked Jul 27, 2011 at 21:26
4

6 Answers 6

22
(1) const char* 
(2) char const* 

This is a pointer (that you can change) to a char (or multiple chars) that you cannot change. In other words, all string literals.

(3) const char const*

This is doubled up. I think you were trying to go for the third position:

(4) const char * const

which is a pointer that you cannot change, to a char (or multiple chars) that you cannot change. You can use this for global pointers to literals that should not be accidentally changed.

The string literals are going to be (most likely) lumped right after your code, usually in a segment or section called "rodata".

answered Jul 27, 2011 at 21:30
11

1 and 2 are equivalent, and specify the type of a pointer to a const char. The pointer itself is not const. 3 is invalid, because it repeats "const". It's like saying const const int. The order is not relevant, so it's also like saying int const int.

In C99 it is valid to repeat const like that. But in C++, you cannot repeat it.

Also, how are string-literalls stored by the compiler?

They are stored in an unspecified way. But compilers are allowed to store them in a read-only portion of the program. So you cannot write to string literals. You are guaranteed that they stay allocated through the whole program lifetime (in other words, they have static storage duration).

This isn't homework, I'm just brushing up on C for interviews in case anyone cares.

You should be aware of the subtle differences between C and C++. In C99, like explained above, const const int is allowed. In C89 and C++ it is forbidden. In C++ you can however introduce a redundant const, if applied to a typedef that is itself const:

typedef int const cint;
cint const a = 0; // this const is redundant!

Same goes for template parameters.

answered Jul 27, 2011 at 21:28
8
  • const char * and char const * have the same meaning: the pointed value is const, but the pointer itself can be changed to another address. After initialization, *p = 'X'; is invalid (does not compile), and p = &x; (where x is a variable of type char) is valid (it compiles).

  • So const char const * indicates twice the same thing.

  • char * const: the pointed value can be changed, but the pointer itself is const. It cannot be modified to point to another address. After initialization, *p = 'X'; is valid, and p = &x; is not.

  • const char * const and char const * const have the same meaning: both pointed value and pointer are const. Neither *p = 'X'; nor p = &x; are valid after initialization.

answered Jul 27, 2011 at 21:47
7

There are quite a lot correct answers here but you may find it hard to remember, here is a trick to memorize this :

1> when const is on the left hand side of *, it means the pointer points to a constant object;

e.g.const int * p means the int cannot be changed via pointer p

2> when const is on the right hand side of *, it means the pointer is a const pointer;

e.g.int * const p means p is a constant pointer which cannot be changed.

BTW if you have const on both side of *, then it means it's a const pointer and you cannot change the object via the pointer.

e.g. int const * const p

answered Jul 27, 2011 at 21:52
2
  1. const char * Pointer to constant char data (read right to left).

  2. Same as #1.

  3. const char * const Constant pointer (pointer can't be changed) to constant data (data cannot be modified).

  4. String literals are stored in read-only memory, usually as is, terminated by a '0円'. They are constant pointers to constant data.

answered Jul 27, 2011 at 21:33
1
  • 1
    string literals are not pointers, they are arrays. Consider sizeof "foobar" Commented Jul 27, 2011 at 21:53
1
  1. const char* means pointer to constant character
  2. char const* means exactly the same as 1. (you probably were going for char* const, which is constant pointer to a character.)
  3. const char const* is invalid as Johannes already pointed out

String literals are usually stored in a read-only data segment of your executable, but this is by no means guaranteed.

answered Jul 27, 2011 at 21:31
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.