Question

I was looking at C99 specification (N1256.pdf) which says on (p.11506):

const int *ptr_to_constant;
int *const constant_ptr;

"The contents of any object pointed to by ptr_to_constant shall not be modified through that pointer, but ptr_to_constant itself may be changed to point to another object. Similarly, the contents of the int pointed to by constant_ptr may be modified, but constant_ptr itself shall always point to the same location." (6.7.5.1 Pointer declarators)

Now from what I read earlier the following two statements give rise to identical behavior.

int *const constant_ptr; /* This form is mentioned in the standard */
int const *constant_ptr; /* This form is NOT mentioned in the standard */

I was wondering if the second form is correct or just an extension.

Thanks in advance, -S

Was it helpful?

Solution

Actually int const *constant_ptr; is the same as const int *ptr_to_constant;. The const keyword affects the element at left, if there's not, it will affect the element at right.

int const *constant_ptr;, here the element at the left of const is int.

const int *ptr_to_constant;, here const doesn't have an element at left, so it applies to te right one, which is int.


const int *ptr_to_constant; 

Here, only the value pointed by the pointer is constant.

int *const constant_ptr;

Here, the pointer is constant.

int const *constant_ptr;

Here, only the value pointed by the pointer is constant.

int const * const constant_ptr_to_constant;

Here, the pointer and the value pointed by it are constants.

EDIT:

int const *constant_ptr;, you call the pointer constant_ptr, but if I keep your name scheme, it should be called ptr_to_constant.

OTHER TIPS

If the `const' keyword is to the left of the asterisk, and is the only such keyword in the declaration, then object pointed by the pointer is constant, however, the pointer itself is variable.

int a = 1;
int b = 2;
const int *p1;
p1 = &a;
p1 = &b; // Can be pointed to another variable
*p1 = 23; // <----- NOT ALLOWED

If the `const' keyword is to the right of the asterisk, and is the only such keyword in the declaration, then the object pointed by the pointer is variable, but the pointer is constant; i.e., the pointer, once initialized, will always point to the same object through out it's scope.

int a = 1;
int b = 2; 
int * const p2 = &a;
*p2 = 7; // <----- Can assign a value via indirection
p2 = &b; // <----- NOT ALLOWED

If the `const' keyword is on both sides of the asterisk, the both the pointer and the pointed object are constant.

int a = 1;
int b = 2;
const int * const p3 = &b;
*p3 = 42; // <------ NOT ALLOWED
p3 = &a; // <------ NOT ALLOWED

The "const" keyword modifies different things in these two cases.

"const int *" means that it's the "int" part that can't change.

"int *const" means that just the variable value itself (the pointer) cannot be changed.

This is stated in the text you quote, but in a more sophisticated way.

Try doing some assignments and see what errors, you'll get the idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top