Notice: Undefined variable: meta in /home/customer/www/demo.emertxe-group.com/public_html/wp-content/themes/Divi-child/functions.php on line 1109

Deprecated: implode(): Passing glue string after array is deprecated. Swap the parameters in /home/customer/www/demo.emertxe-group.com/public_html/wp-content/plugins/divi-filterable-blog-module/includes/controller/autoloader.php on line 94
Pointer Constant & Constant Pointer - Differences - Emertxe Custom

Emertxe-News & Blog

Home » C » 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 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’.

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

Declaration

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


Illustration

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,

 
 

In Brief:

Table-2: Briefing pointer to constant concept

 


Constant Pointers

Aconstant pointer is a pointer that cannot change the address it iscontaining. 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’

 

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

Table-6
 

Enjoy C programming 🙂

Best Embedded systems training institute with placements in Bangalore.

YOU MAY ALSO LIKE

The Future of Embedded Systems : Trends and Predictions

The Future of Embedded Systems : Trends and Predictions

Introduction Embedded systems have become an integral part of modern technology, and their importance is increasing day by day. These systems are designed to perform specific tasks and are embedded in everyday devices, making them intelligent and interactive. From...

Online Free IoT Internship for Engineering Students – 2023

Online Free IoT Internship for Engineering Students – 2023

The Internship Ecosystem An internship is a type of professional learning opportunity where engineering students get practical work related to their field of study or career interest. Through an internship, a student can explore and advance their career while...

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *