Const Qualifier in C
In general, the const qualifier is used to declare a variable as constant, meaning its value cannot change once the variable has been initialized. However, there are several benefits of using const, such as if we have a constant value of the PI, we wouldn't like any part of the program to modify that value. Therefore we can declare it as a const using the const qualifier. But it has a little twist as usual because sometimes we are able to change the values of variables that are declared with const qualifiers. However, this is not always possible as it depends on where the const variables are stored in memory.
Syntax of const variable
In General, the const qualifier only gives only an indication to the compiler that the value of qualify object cannot be changed. In other words, we can say that the const means not modifiable (cannot assign any value to the object at the run time).
However, when we declare an identifier as constant using the const qualifier, it depends on the implementation (or on the compiler) where the "constant variable" will go or stored in the process control block. Thus several machines store the constant and jump tables in the text section, which only reads and contains all other executable instructions.
There are few rules that can help us decide when to use a const in the C program:
Now, let us see some examples in which we will use the const qualifier in our program with a pointer:
1. Pointer to constant
As you can see in the above declaration that "xData" is pointing to a constant integer variable. Therefore we cannot change the value of the pointed integer variable using the pointer (*xData), but we can change the pointer to point to any other integer variable. So now it is possible that, the pointer can point to any other variable because it is usually stored in the R&W area (or read and write memory). So lets us see an example of how we can implement it with the help of following the given examples.
In this example, we will try to change the value of the integer variable using the pointer to constant (piIndex). However, If a user tries to change the value of the variable (iIndexData1) using the *piData, he will get the compiler error.
In function 'main':error: assignment of read-only location '*piIndex'
*piIndex = 3;
Now, we will try to change the pointing variable in this example.
*piIndex is 2 *piIndex is 4
You can see that the above give code works perfectly, and we have successfully changed the pointing variable:
2. Constant pointer to a constant:
As you can see in the above declaration, it is described that the constant pointer is pointing to a constant integer variable. This means we cannot change the value pointed by the pointer, and we can also not point the pointer to other integer variables.
In this example, we will use the concept of a constant pointer to a constant.
*piData is 2
As you can see that everything is working fine if we don't try to change the value of piData and *piData.
In this example, we will try to change the value of *piData. However, according to the concept, we will get the compiler error because *piData qualify to constant. Now let's see what happens:
Error: assignment of read-only location '*piData'
As you can see in the Output, we cannot change the value of "*pidata".
Here we will see that can we can point another integer variable to the pointer. Lets us see what happens:
Error: assignment of read-only variable 'piData'
In the Output of both above given programs, you can see that in case of constant pointer to constant, we cannot change the constant value and neither we can point the constant pointer to another variable.