1

Possible Duplicate: What is the difference between const int*, const int * const, and int const *?

What is the difference between the following?

char const *p;
const char  *p;
char *const p;

And is there a good site where I can relearn C and C++? It seems that I forgot it and job interviews are doing me an hard time...

Community
  • 1
  • 1
yoavstr
  • 367
  • 1
  • 6
  • 15
  • Online resources can't compare to [good books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Georg Fritzsche May 24 '10 at 21:37
  • 3
    Many, many duplicates on SO already, e.g. [what is the difference between const int*, const int * const, int const *](http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-int-const) – Paul R May 24 '10 at 21:40
  • On Posix, there is a program called `cdecl` which you can use. `cdecl explain 'const char *p'` will answer `declare p as pointer to const char` – R Samuel Klatchko May 24 '10 at 21:48

7 Answers7

17

The first two are the same.

The trick is, read it backwards....

So the first one is:

backwards: p * const char
read:      p is a pointer to a const char
meaning:   you can change p to point a at something else, but you can't
           change what it points at

And the last one is:

backwards: p const * char
read:      p is a const pointer to a char
meaning:   p is a pointer which you can't change what it points at, but
           you can change the thing it points to.
egrunin
  • 24,650
  • 8
  • 50
  • 93
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
3

I quite like cplusplus.com for reference and learning.

mpen
  • 272,448
  • 266
  • 850
  • 1,236
3

char const * and const char * are the same. The pointer is non-const (you can modify the pointer), but the chars are const (you cannot change the characters they point to). You can do this

p += 1;

but not this

*p += 1;

char * const is a const pointer to non-const chars. This means you can do this

*p += 1;

but not this

p += 1;
tc.
  • 33,468
  • 5
  • 78
  • 96
2

The first two are the same and means a pointer to const char. The second is a constant pointer to (non-const) char. In the first case, you can/could change the pointer, not but what it points at. In the second case, you can change what it points at, but not make the pointer refer to different memory.

The main thing with pointer declarations/definitions is to keep track of the '*':

char X * Y ptr;

Either 'X' or 'Y' can be replaced by const, or volatile, or both. The 'X' replacement will modify what the pointer points AT. The 'Y' replacement will modify the pointer itself. On the 'X' part, you can have X char or char X -- this makes no difference at all. What makes a difference is placement relatively to the '*'. If the modifier is next to the name of the type, it modifies what the pointer points at. If the modifier is next to the name of the pointer, then it modifies the pointer itself.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

char const * (char const *): You cannot change the value this points to via this pointer. Ergo: constant pointer.

char *const: You can only initialize to a pointer of something - cannot change later on.

This code illustrates it:

int main()
{
    char a = 'a';
    char b = 'b';

    char const *p1; //p1 is the same as p2 const char = char const
    const char  *p2;
    char *const p3 = &a; //Can only assign like this.

    p1 = &a;
    p2 = &a;

    *p1 = b; //Will not compile - cannot change the value via the pointer.
    *p2 = b; //Will not compile - cannot change the value via the pointer.

    p3 = &a; //Will not compile - cannot reassign the pointer

    printf("p1 = %c\n",*p1);
    printf("p2 = %c\n",*p2);
    printf("p3 = %c\n",*p3);

    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LoudNPossiblyWrong
  • 3,855
  • 7
  • 33
  • 45
0
  1. Buy a copy of Kernighan & Ritchie (K&R) and work through it.
  2. Do the exercises in K&R and understand the solutions provided in The C Answer Book.
  3. Buy a copy of Accelerated C++ and work through it, because it treats C++ as its own language and not just C with "bits bolted on".
  4. Buy a copy of Effective C++, don't violate the items and read each one to understand why you shouldn't violate them.

As an added bonus, read Effective STL to use the STL properly.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rob Wells
  • 36,220
  • 13
  • 81
  • 146
0

There are two separate things here, a pointer and the data it points to, and const can be used for either or both.

Example without const

char *p = "hello";
++p;              // Changes the pointer, now points to 'e'
*p = 'u';         // Changes the data, now "hullo"

Examples with const

As @tc says, read the declaration backwards.

  1. You can change the pointer, but not data it points to

    // These two declarations have the same effect
    char const *p = "hello";  // Pointer to constant char
    const char *p = "hello";  // Pointer to char which is constant
    ++p;              // OK
    *p = 'u';         // Won't compile
    

2 You can change "hello", but not the pointer

    char * const p = "hello"; // Constant pointer to char
    ++p;              // Won't compile
    *p = 'u';         // OK
  1. You can make both immutable

    char const * const p = "hello"; // Constant pointer to constant char
    ++p;              // Wont' compile
    *p = 'u';         // Wont' compile
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
egrunin
  • 24,650
  • 8
  • 50
  • 93