Q

Emertxe-News & Blog

Home » Blog » Pointer Constant & Constant Pointer – Differences
Pointer Constant & Constant Pointer – Differences
 

Introduction:

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 constin C programming. In this blog I want to focus on difference Pointer Constant Vs Constant Pointer in C programming.

 

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’.

 
Note 1: But these pointers can change the address they point tobut cannot change the value at the address they are ‘pointing to’.
 

Declaration

Pointer Constant Vs Constant Pointer in C programming
Table-1: showing how to declare ‘pointer to constant’
 

 

Illustration

Let us consider the code snippet to understand how ‘pointer to constant’ works,

Pointer Constant Vs Constant Pointer in C programming
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.

 
Pointer Constant Vs Constant Pointer in C programming
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.

 
Pointer Constant Vs Constant Pointer in C programming
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,

 
 
Pointer Constant Vs Constant Pointer in C programming
 

In Brief:

Briefing pointer to constant concept
Table-2: Briefing pointer to constant concept

 


Constant Pointers

Aconstant pointer is a pointer that cannot change the address it is containing. In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable. 

 

 

Note 2: But these pointers can change the valueof the variable theypoint tobut cannot change the address they are ‘holding‘.

Declaration

Table-3: showing how to declare ‘constant pointer’
 

 

Illustration

Let us consider the code snippet to understand how ‘constant pointer’ works,

Fig-7: Illustration example for 'constant pointer'
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,

 
 

In Brief:

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.

 

Application

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.

 
 
Example1:
 
Fig-12 : Shows the usage of pointer to constant in strlen() library function.
 
 
 
 
Example2:
 
Fig-13 : Shows the usage of pointer to constant in strcmp() library function.
 

Summary

Table-5
 

 

Trick: How to understand the differences Pointer Constant Vs Constant Pointer in C programming

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.

Table-6
 

Enjoy “C programming”

YOU MAY ALSO LIKE

Emertxe’s Online Training Programs in Embedded Systems & IoT

Emertxe’s Online Training Programs in Embedded Systems & IoT

The reason why Emertxe is the best choice for career advancement: eLearning and Online training programs are the way forward in the COVID-19 disrupted world. Riding along the digital revolution will ensure engineers are future-ready with skills to not only secure but...

Our Training Programs for Freshers

Our Training Programs for Freshers

Introduction: Emertxe is the leading training institute in Bangalore for Embedded Systems and IoT domains. It is a pioneer in training freshers since 2003 by providing excellent placement opportunities for freshers. Over the years 70000+ students have made their...

Q