Jump to content
 
  • 0

pointer to const vs. const pointer


Orunmila

Question

  • Member

I came across this one yet again today, so I wanted to record it here where people can find it and where I can point to it without looking up all the details in the standard over and over again.

I know pointers are hard enough to grok as it is, but it seems that const-qualified pointers and pointers to const-qualified types confuses the hell out of everybody.

Here is a bit of wisdom from the C standard. As they say - if all else fails read the manual!

Quote

§6.7.5.1

EXAMPLE The following pair of declarations demonstrates the difference between a ‘‘variable pointer to a constant value’’ and a ‘‘constant pointer to a variable value’’.


          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.

 

BTW. The same applies to all qualifiers so this counts for volatile as well. I see the mistake too often where people are trying to make a pointer which is being changed from an ISR e.g. and they will use something like:

volatile List_t * ListHead;

This usually does not have the intended meaning of course. The volatile qualifier applies to the List_t and not to the pointer. So this is in fact not a volatile pointer to a List_t. It is instead a non-volatile pointer to a "volatile List_t". In simple terms it is the variable at the address being pointed to which is volatile, not the address itself (the pointer). 

To make the a volatile pointer, that is a pointer which is changed from another context such as an ISR, you need to do it like this:

List_t * volatile ListHead;

Of course if both the pointer and the thing it is pointing to are volatile we do it like this:

volatile List_t * volatile ListHead;

 

There is another example in section 6.2.5 of the standard.

Quote

EXAMPLE 1 The type designated as ‘‘float *’’ has type ‘‘pointer to float’’. Its type category is pointer, not a floating type. The const-qualified version of this type is designated as ‘‘float * const’’ whereas the type designated as ‘‘const float *’’ is not a qualified type — its type is ‘‘pointer to const- qualified float’’ and is a pointer to a qualified type.

 

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

For me, the trick to strictly read it from right to left helped a lot.

volatile List_t * ListHead;
  • A variable ListHead
  • which is a pointer
  • to the type List_t
  • which is volatile.

 

 

List_t * volatile ListHead;
  • A variable ListHead
  • which is a volatile pointer
  • to a type List_t

 

 

volatile List_t * volatile ListHead;
  • A variable ListHead
  • which is a volatile pointer
  • to a type List_t
  • which is volatile

 

 

const int *ptr_to_constant;
  • A variable ptr_to_constant
  • which is a pointer
  • to a type int
  • which is a constant

 

 

int *const constant_ptr;
  • A variable constant_prt
  • which is a constant pointer
  • to a type int
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

 


×
×
  • Create New...