Pointers are always been a complex topic for the newbies into the C programming world. It will be more confusion for the newbies when these ‘sharp knives‘ are used along with some qualifiers like ‘const‘in C programming. In this blog I want to focus on difference between ‘pointer to constant’ & ‘constant pointer’ in order to make the concept very clear.
General Note :
code snippets provided here is tested with GCC compiler [ gcc version 4.8.2 ] running under Linux environment.
Pointer to constant
As the name itself indicates that the value of the variable to which the pointer is pointing is constant in other words a pointer through which one cannot change the value of variable it points is known as a ‘pointer to constant’.
|Table-1: showing how to declare ‘pointer to constant’
Let us consider the code snippet to understand how ‘pointer to constant’ works,
|Fig-1: Illustration example for ‘pointer to constant’
In the above example, at line no.12 we are trying to change the value of the variable to which the pointer is ‘pointing to’, but this is not possible since the value is constant. When the above code is compiled & run, we are getting the output as shown in the below figure.
|Fig-2: Output of the code snippet given in Fig-1
Now we will take the same example to show that the ‘address’ the pointer is containing is not a constant.
|Fig-3: Demonstration example to show that the address is not constant.
From the above figure, one can understand that the ‘address’ where the pointer is containing can be changed but not the value. This can be clearly understood by the pictorial representation given below,
Table-2: Briefing pointer to constant concept
Note 2: But these pointers can change the valueof the variable they‘point to‘but cannot change the address they are ‘holding‘.
|Table-3: showing how to declare ‘constant pointer’
Let us consider the code snippet to understand how ‘constant pointer’ works,
|Fig-7: Illustration example for ‘constant pointer’
From the above fig, it is clear that at line no. 14, we tried to change the address of the pointer ‘ptr’ to some other variable,but according to the concept it is not possible. The output of the above code snippet is given below. Similarly, one can observe at line no. 12, we are trying to change the value of the variable it is ‘pointing to’ which is possible.
|Fig-8: Output of the code snippet given in Fig-7
This can be clearly understood by the pictorial representation given below,
Table-4: Briefing ‘constant pointer’ concept
Similarly, we can have both pointer to constant & constant pointer in a single statement. This is left as an exercise to analyze.
We can find ‘n’ number of applications of these concepts in ‘C’ as well as ‘Embedded C’Programming world. One such simple application of ‘pointer to constant’ is to find the string length of the given string without any attempt to modify the original string as shown in Example 1 and Example 2 gives an idea of usage of ‘pointer to constant’ in strcmp() function.
|Fig-12 : Shows the usage of pointer to constant in strlen() library function.
|Fig-13 : Shows the usage of pointer to constant in strcmp() library function.
Trick: How to understand the differences between pointer to constant & constant pointers.
Note 3: This trick is for all the newbies for the C programming world , who are confusing with constant& pointers.
From the above summary, separate the part before asterisk(*) & part after the asterisk(*) as given in the below table-6.
Enjoy C programming 🙂