Article to interpret const and volatile

Use of 'const' and 'volatile'

Applies to: Software Development Toolkit (SDT)
Description
The correct placement of the type qualifiers 'const' and 'volatile' can sometimes be confusing, especially when applied to a pointer or the thing it points to.
Solution
Here is a simple rule which may help: place 'const' (or 'volatile') *after* the thing you're saying is constant (or volatile).
  • to declare a constant integer, use 'int const xxx', instead of 'const int xxx'.
  • to declare a pointer to a constant integer, put 'const' after 'int' to get 'int const *ptr'.
  • to declare a constant pointer to an integer, put 'const' after '*' and get 'int * const ptr'.
  • to declare a constant pointer to a constant integer, use 'int const * const ptr'.
  • to declare a constant pointer to a volatile integer, use 'int volatile * const ptr'.
The really nice thing about doing it this way is that you can see what the type is by simply reading *backwards* through the definition:
int const xxx;
|    |    |
|    |    +-----------------> xxx is a
|    +----------------------> constant
+---------------------------> integer 
int const *ptr;
|     |  | |
|     |  | +----------------> ptr is a
|     |  +------------------> pointer to a
|     +---------------------> constant
+---------------------------> integer 
int * const ptr;
|   | |     |
|   | |     +---------------> ptr is a
|   | +---------------------> constant
|   +-----------------------> pointer to an
+---------------------------> integer 
int const * const ptr;
|    |   |   |    |
|    |   |   |    +---------> ptr is a
|    |   |   +--------------> constant
|    |   +------------------> pointer to a
|    +----------------------> constant
+---------------------------> integer 
int volatile * const ptr;
|     |     |   |    |
|     |     |   |    +------> ptr is a
|     |     |   +-----------> constant
|     |     +---------------> pointer to a
|     +---------------------> volatile
+---------------------------> integer 
For a real example of the application of 'const', see Placing constant jump tables in ROM
For an example of the use of 'volatile', see armcc/tcc: Placing C variables at specific addresses - memory-mapped peripherals
In fact, this method works for any type qualifier, including __packed, e.g. int __packed x and int *__packed x;

taken from http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3736.html

Simple Rule : Read it right to left
  • int* - pointer to int
  • int const * - pointer to const int
  • int * const - const pointer to int
  • int const * const - const pointer to const int
Now the first const can be on either side of the type so:
  • const int * == int const *
  • const int * const == int const * const
If you want to go really crazy you can do things like this:
  • int ** - pointer to pointer to int
  • int ** const - a const pointer to a pointer to an int
  • int * const * - a pointer to a const pointer to an int
  • int const ** - a pointer to a pointer to a const int
  • int * const * const - a const pointer to a const pointer to an int
  • ...
And to make sure we are clear on the meaning of const
const int* foo;
int *const bar; //note, you actually need to set the pointer 
                //here because you can't change it later ;)

From stack overflow...


Comments

Popular posts from this blog

Airtel Digital tv remote factory reset and reprogram

Tracking Linux kworker threads

Asynchronus I/O (AIO) in vxworks and Linux