2

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

Quiescent
  • 1,088
  • 7
  • 18

3 Answers3

5

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.

nouney
  • 4,363
  • 19
  • 31
  • Thanks nouney. Could you suggest a reference to this behavior? – Quiescent Aug 12 '13 at 10:49
  • @user926918 [This question](http://stackoverflow.com/questions/5503352/const-before-or-const-after) may interrested you. – nouney Aug 12 '13 at 10:53
  • It's true there was interesting discussion on possible reasons for parsing but it does not address my trouble. I rechecked rules for qualifiers again in the C99 standard but, as far as I have noticed, is silent on the issue. That is, it does not say the last statement is wrong or unspecified or undefined. So it gives me a suspicion that the practice seems to be an extension to the standard. – Quiescent Aug 12 '13 at 10:59
  • 2
    The notations `int const *ptr` and `int * const ptr` are both fully standardized behaviour (but are different from each other). What you quote in the question is an example (technically, that's non-normative, therefore, but it doesn't matter for this question) in section 6.7.6.1 Pointer declarators of the 2011 standard (ISO/IEC 9899:2011). The syntax in section 6.7.6 Declarators clearly shows '`*` type-qualifier-list-opt `D`' where `D` is a declarator and 'type-qualifier-list-opt' allows for `const` and other qualifiers. – Jonathan Leffler Aug 12 '13 at 11:18
  • Thanks Jonathan Leffler. I looked at the section (6.7.5 for C99) and it looks formidable and demands more time. Thanks for the reference. – Quiescent Aug 12 '13 at 11:44
2

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
EnterKEY
  • 1,180
  • 3
  • 11
  • 25
1

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.

Brad Peabody
  • 10,917
  • 9
  • 44
  • 63
  • 1
    There seems to be some confusion. I was asking the about the second form given in the standard vs one that is not given the standard. I shall mention this explicitly in the question. – Quiescent Aug 12 '13 at 10:04
  • @user926918 Everything you mentioned is in the standard -- you're misunderstanding it. – Jim Balter Aug 12 '13 at 12:24
  • You're right - my bad. I'm not sure if "int const *constant_ptr;" is totally standard (or which version of the standard it does or doesn't comply to), but I've seen that used in many places and is definitely used in practice and should compile on any modern C/C++ compiler. – Brad Peabody Aug 12 '13 at 22:27