Tuesday, 16 December 2014

Pointer To Constant, Constant Pointer, Combination Of Both i.e. Pointer To Constant + Constant Pointer

To understand the concept of pointer for beginner very hard and complex. and to understand with constant qualifier bit confusing so i written this blog to understand the concept of pointer to constant, constant pointer, and combination of both i.e. pointer to constant + constant pointer.
 

1) Pointer To Constant

   As the name itself telling you the value of the variable to which pointer is pointing  is constant.
   i.e. using that pointer we can not change the value of that variable to which it point is  called 
   pointer to constant.
 

How to declare pointer to constant

   There are two way to declare this type of pointer

  
   That mean you can change the order of const and type of pointer to declare pointer to 
   constant see above example.

Now you should understand following two things

   1) These pointer can change the address they point to (see below program 1).
       mean pointer can change the address they are holding.
       mean once the pointer pointing to some variable it can also point other variable afterwords.
   2) But can not change the value of the variable they pointing to (see below program 2).
 

To understand above two concept see the following graphical representation



Table shows the concept of pointer to constant

 


2) Constant Pointer

   A constant pointer is one that cannot change the address it contains (i.e. holding address).
   i.e. once the pointer point to a variable it can not point to other variable afterwords.

How to declare constant pointer

 

   There is only one way to declare this type of pointer  

  

Now you should understand following two things

   1) These pointers can change the value of the variable they point (see below program 2).

   2) But cannot change the address they holding (see below program 1).
       i.e once the pointer point to some variable
afterwords it can not point to other variable.


To understand above two concept see the following graphical representation





Table shows the concept of constant pointer








3) Pointer to constant + Constant pointer (combination of both)

   mean we can combine this two things so we get
   value of the variable to which pointer is pointing  is constant.
   i.e. using that pointer we can not change the value of that variable to which it point.
   and,
   we cannot change the address it contains.
   i.e. once the pointer point to a variable it can not point to other variable afterwords.

How to declare combination of both

 

   There is one way to declare this type of pointer
  

   

 

Now you should understand following two things

 

   1) These pointer cannot change the address they holding (see below program 1).
       i.e once the pointer point to some variable
afterwords it can not point to other variable.

   2) Also pointer can not change the value of the variable they pointing to (see below program 2).    

 

To understand above two concept see the following graphical representation

 




Table shows the concept of both combination

 

Summary Example


Now must read if you are beginner

   Till some beginner are face the problem to declare and  tracing the technical aptitude 
   output of pointer to constant, constant pointer, and combination of both.
   so i will give you some trick to understand this concept.

   when this type of declaration you required or want to trace the output just divide 
   the declaration  where the * present.
   mean see the following diagram

Now,

if the const is associated with a data type mean value is constant i.e.(pointer to constant).

if the const is associated with a pointer then pointer is constant i.e.(constant pointer).


Summary Example

 

Usage 

    To find the string length of a given string without modifying the string same as for
    comparing to strings.



 

Thanks For Reading The Content

if you have any doubt please free to comment below i try to solve it

and if there is any mistake in blog please mail me or comment below.