Pointer Constants

A pointer or pointed-at object can be declared with the const modifier. Anything declared as const cannot change its value. It is also illegal to create a pointer that might violate a non-assignability of the constant object.

Consider the following examples:

int i;                         // i is an int
int * pi;                      // pi is a pointer to int (uninitialized)
int * const cp = &i;           // cp is a constant pointer to int
const int ci = 7;              // ci is a constant int
const int * pci;               // pci is a pointer to constant int
const int * const cpc = &ci;   // cpc is a constant pointer to a
                               //     constant int

The following assignments are legal:

i = ci;                       // Assign const-int to int
*cp = ci;                     // Assign const-int to
                              //     object-pointed-at-by-a-const-pointer
++pci;                        // Increment a pointer-to-const
pci = cpc;                    // Assign a const-pointer-to-a-const to a
                              //     pointer-to-const

The following assignments are illegal:

ci = 0;                       // NO--cannot assign to a const-int
ci--;                         // NO--cannot change a const-int
*pci = 3;                     // NO--cannot assign to an object
                              //     pointed at by pointer-to-const.
cp = &ci;                     // NO--cannot assign to a const-pointer,
                              //     even if value would be unchanged.
cpc++;                        // NO--cannot change const-pointer
pi = pci;                     // NO--if this assignment were allowed,
                              //     you would be able to assign to *pci
                              //     (a const value) by assigning to *pi.

Similar rules are applayed to the volatile modifier. Note that both const and volatile can appear as modifiers to the same identifier.

  Notes :
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code