14.7 – const qualifier in C language

by subbu on January 20, 2014

Const qualifier in C language

The qualifier const can be applied while declaring a variable, an array or an argument to specify that its value will not be changed.
It is generally used to declare constants whose values must not be changes in program.
const objects can be used as a normal objects except restrict to modify
Value is initialized while declaration of const object because a new value can’t be assigned once the const object is declared

const short x=10;

It is the declaration of a const variable, whose value can’t be changed

int main()
{
const short x=10;
printf("\n%d",x);
x=x+45;
printf("\n%d",x);
return 0;
}

Output:
Error: Cannot modify a const object in function main

Here the compiler wont allows to change the value of const variable x. Program would be executed if the statement x=x+45; is released.

int main()
{
const short x=10;
printf("\n%d",x);
/* x=x+10; */
printf("\n%d",x);
return 0;
}

Output:
10
10

const short x[]={10,20,30,40,50};

It is the declaration of a const array, the elements of which can’t be changed

#include<stdio.h>
int main()
{
const short x[]={10,20,30,40,50};
int i;
for(i=0;i<5;i++)
x[i]=x[i]+5;   /* incrementing every element by 5 */
printf("Elements of array:\n");
for(i=0;i<5;i++)
printf("%5d",x[i]);
return 0;
}

Output:

Error: Cannot modify a const object in function main

void change(const int[]);

It informs the compiler that, contents of array should not be changed by the function change().

Here, the formal argument x[] is capable to access and modify the elements of a[] from the function change() but, will not allow to change the elements because a[] is  being send as const argument.

#include<stdio.h>
void change(const short[],int);
int main()
{
short a[]={10,20,30,40,50};
short i;
change(a,5);                    /* sending an array as argument */
printf("Elements of array:\n");
for(i=0;i<5;i++)
printf("%5d",a[i]);
return 0;
}
void change(const short x[],int n)
{
int i;
for(i=0;i<n;i++)
     x[i]=x[i]+10;                 /* chnaging actual argument     */
}

Output:

Error 17: Cannot modify a const object in function change

How to change the const object?

Const is the compiler instruction to not allow modifying the object by it’s name. Without specifying the name of const object, we can modify the const object. One way of doing it is by modifying through its pointer using dereferencing

#include<stdio.h>
int main()
{
const short x=45;
short *p=&x;
*p=*p+40;
printf("\nx=%d",x);
return 0;
}

Output:
x=85

Here x is the const object, p is the pointer to x. *p=*p+40; changes the value of x through dereferencing.

Const pointer:

We know that a const object is an object that can’t be modified. The same way a pointer can be defined as a const pointer to point the same variable that is to store the address of same variable. Compiler throws error if we try to assign the address of another variable to the const pointer.

#include<stdio.h>
int main()
{
short x=10,y=20;
short *const p=&x;
printf("\nx=%d",*p);
p=&y;
printf("\ny=%d",*p);
return 0;
}

Error 7: Cannot modify a const object in function main

Here p is the const pointer initialized with the address of x. It results error when we try to assign the address of another variable y

Though const pointer won’t allow to store the address of another variable, it allows to change the value of variable it points

#include<stdio.h>
int main()
{
short x=10;
short *const p=&x;
printf("\nx=%d",*p);
*p=*p+25;
printf("\ny=%d",*p);
return 0;
}

Output:
x=10
y=35

Sending const pointer as argument:

The best way of sending arguments to a function is by pass by value because constants, expressions and variables can be send as arguments but, copying large objects can take a lot of time to copy, and this can cause a performance penalty, especially if the function is called many times

In case of pass by address, only the address is send as argument rather the total object but, there is a possibility of modifying the actual argument through dereferencing, most of the times which is undesirable.

If we want that a function should not change the value of actual argument, but don’t want pass by value, then best solution is pass by const pointer.

#include<stdio.h>
void display(const short *);
int main()
{
short x=10;
display(&x);
return 0;
}
void display(const short *p)
{
printf("\nx=%d",*p);
*p=*p+10;
}

Output:
Error 12: Cannot modify a const object in function display

Here x is a non const variable in main(), We can always modify its value  from the function display through dereferencing but, could not modify here as the pointer to x is the const pointer argument.

It is best practice of defining formal arguments as const pointers when we send addresses as arguments when change in actual arguments is undesirable.

Summarizing const qualifier:

  • Const objects can’t be changed once they are declared
  • Constant objects can be modified through its pointer indirectly using dereferencing
  • Const pointer can’t point another variable but, can modify the contents of object that it points
  • We can’t modify the actual argument by its const pointer formal argument
  • It is best practice to declare formal arguments as const pointers when passing addresses as arguments

Previous post:

Next post: