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'.
Note 1:
But these pointers can change the
address they 'point
to'
but 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
A
'constant
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
value
of the variable they
'point
to'
but 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 :-)
No comments:
Post a Comment