const qualifier in C language

One thing which I considered to be easy enough not to be taken into consideration but few days ago raised some real issues: const qualifier.

So… what to say about const? Usually the first answer a C programmer gives to this question is const indicates something that cannot change. In reality this is far from being an exhaustive definition of const and it is not even 100% correct.

There are three topics here and just let me take them in order, there are const variables (which is quite straightforward), there are also const functions and const pointers (… and const references). In case of last two, things are complicating a little bit.

Let’s just go back and just slightly correct the assumed definition of const. It is better to say a const variable  is a compile-time fixed variable. By qualifying a variable as const you’re getting a guarantee from the compiler that the variable won’t change through that name.

const int a = 1;
a = 2;

The code above will issue an error (one thing to notice is that const data types always must be initialized) but the one below will not

const int a = 1;
int *b = &a;
*b = 2;

As you may see an object or a data stored somewhere in the memory, can be referenced via an expression.

Here you may find an article exhaustively treating this subject which I strongly recommend you to read. There is a key sentence  highlighted on the first page:

The expression you use to refer an object differs from the object itself.

… and it continues

Sometimes this distinction matters.

Usually a variable is referred directly, through its name, but it can also be referred indirectly, through some reference or pointer (as far as I know those two are the only ways to indicate data stored into memory).

In case of simple variables, as I already said, const qualifier means that data itself cannot be changed via the name that refers it directly, but it doesn’t exclude the possibility of altering the data via other means.

Const pointers are slightly more complicated and usually they are one of the most preferred topics for C related interview questions.

If you’re scheduled for an interview in the near future just pay attention at the following lines:

int const *p; // pointer to const
int * const p; // const pointer
int const * const p; //const pointer to const

My technique is to read from right to left, replacing * with pointer word.

Pointer to const means that, using the earlier terms – expression to object and object itself – expression to object can change, but the object itself cannot be changed via the pointer.

int c = 5;
int const *pc = &c;
*pc = 4; // this will issue an error, because the object cannot be changed
pc = &d; // supposing that d is another integer defined earlier
// pc can be changed to point to something else

But however…

c = 4; //will compile OK because data can be changed via its own name

Const pointer means that object itself can be changed via the expression, but the expression that refers it cannot change.

int c = 5;
int * const pc = &c;
*pc = 4; // this is OK
pc = &d; // supposing that d is another integer defined earlier
// this will issue an ERROR, pc have to stick to c, it cannot indicate something else


int c = 5;
int const * const pc = &c; //here nothing cannot change, neither the object itself
// neither the expression that points to it

So … after this bunch of exercises did you got the meaning of const?

There is something called qualification conversion (conversion between const or non-const qualified objects and expressions).

Suppose we have following code:

int const c = 6; // via this declaration I am locking c object from being modified
int *pc = &c; //will violate this locking and has to be flagged as an ERROR

Somehow in the memory layout of the program this thing is reflected as placing the const variables in the non-volatile memory (ROM or Flash), well making also the corresponding linker setting. If a pointer as pc, is pointing to a non-volatile memory location, this can generate problems. Code within non-volatile memory cannot be changed during execution and as a consequence the compiler must precautionary flag such unwanted declarations as

int *pc = &c; // non-const pointer to non-const object

Error should look like this:

line xyz: error: a value of type "const int *" cannot be used to initialize an entity of type "int *"

Well… that’s all folks …. see you in next post when I will try to demystify C++ const objects and methods.


2 Responses to const qualifier in C language

  1. Pingback: const keyword in C++ « Bazaar 2.0

  2. Rama says:

    EnglishLanguage is very difficult to understand which is used in explanation of c language. Please specify any other website which is explained with very simple english. language .

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: