views:

208

answers:

7

Possible Duplicate:
what is the difference between const int*, const int * const, int const *

what is the difference between :

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 doing me an hard time ...

+2  A: 

I quite like cplusplus.com for reference and learning.

Mark
+11  A: 

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.
Keith Nicholas
+2  A: 

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
+3  A: 

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.
A: 

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;   
}
LoudNPossiblyRight
A: 
  1. Buy a copy of K'n'R and work through it.
  2. Do the exercises in K'n'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

HTH

Rob Wells
A: 

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';         // wont' compile

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

char * const p = "hello"; // constant pointer to char
++p;              // wont' compile
*p = 'u';         // ok

3 You can make both immutable

char const * const p = "hello"; // constant pointer to constant char
++p;              // wont' compile
*p = 'u';         // wont' compile
egrunin