C Datastructures Networking OS

Saturday 20 April 2013

Usage of constant qualifier with pointer

Difference between  const * int ptr  and   int * const ptr...

First of all we need to understand what const qualifier is and how it is used.
            Declaration of variable with const qualifier specifies that its value will not be changed. It depends upon where const variables are stored ,we may change value of const variable by using pointer.The result is implementation-defined if an attempt is made to change a const.
                  #include <stdio.h>
                 #include<conio.h>
                 void main()
                 {
                          const int a=100;
                          a+=100;                                //error
                          printf("value of a=%d",a);
                             
                  }
               
 o/p: assignment of read only variable 'a'
1)pointer to variable:-
          
                     int *ptr;
            In the above declaration the value of both variable and pointer can be changed.Both the pointer and the value pointed by pointer will be stored in read-write area of memory.

                 #include <stdio.h>
                 #include<conio.h>
                 void main()
                 {
                           int a=100,b=200;
                           int *ptr;
                           ptr=&a;                                        //pointer to integer
                           printf("value of ptr=%d",*ptr);
                           ptr=&b;                                       //pointing to another variable
                           printf("value of ptr=%d",*ptr);
                           *ptr=500;                                   //changing the value of pointer
                            printf("value of ptr=%d",*ptr);
                  }
               
           o/p:
                     value of ptr=100;
                     value of ptr=200;
                     value of ptr=500;
2) Pointer to constant :
          const int * ptr;
                  or
          int const * ptr;

We change pointer to point to another variable,but we cannot change the value of the variable which is pointed by the pointer.Pointer is stored in read-write memory and variable is stored in read-only memory.
                       
                 #include<stdio.h>
                 #inlcude<conio.h>
                 void main()
                 {
                         int i=10,j=20;
                         const int *ptr=&i;
                         printf("Value of ptr=%d",*ptr);
                         
                         ptr=&j;                                             //valid
                         printf("value of ptr=%d",*ptr);
               
                        *ptr=100;                                           // error.
                         printf("value of ptr=%d",*ptr); 
                 }
            
o/p:- error.assignment to read only location *ptr
3)Constant Pointer to variable:-

                int  *const ptr;

             We can change the value of variable pointed by the pointer, but we cannot change the pointer to point to another variable.

                       
                 #include<stdio.h>
                 #inlcude<conio.h>
                 void main()
                 {
                         int i=10,j=20;
                         int * const ptr=&i;
                         printf("Value of ptr=%d",*ptr);
                         
                         *ptr=100;                                           // valid.
                         printf("value of ptr=%d",*ptr); 
                         
                         ptr=&j;                                             //error
                         printf("value of ptr=%d",*ptr);
               
                        
                 }     

         o/p: error in assignment to read only location ptr
4) constant pointer to constant variable:
              We cannot change both the value of variable pointed by the pointer and  also pointer to point to another variable.

                 #include<stdio.h>
                 #inlcude<conio.h>
                 void main()
                 {
                         int i=10,j=20;
                         const  int * const ptr=&i;
                         printf("Value of ptr=%d",*ptr);
                         
                         *ptr=100;                                           // error.
                         printf("value of ptr=%d",*ptr); 
                         
                         ptr=&j;                                             //error
                         printf("value of ptr=%d",*ptr);
               
                        
                 } 
 o/p:     error.assignment to read only location *ptr
            error in assignment to read only location ptr

No comments:

Post a Comment