Tuesday 23 April 2013

Constant Pointer

  1. int const* ptr;
  2. const int*ptr;
  3. int *const ptr;
  4. const int* const ptr;
  5. int const*const ptr;

At the first glance these stuffs looks so scary but they are not as much as we think. Here is two things one is a integer pointer and another is a const keyword. Nothing I need to say about integer pointer and const in brief is used to create a read only variable. One thing before going further I want to say about the above expressions is that you can use the first const on either side of the type (integer here). That means :
int const *ptr is equivalent to const int *ptr and const int* const ptr is equivalent to int const*const ptr. We can easily understand the meaning of each of the above expression if we read these expression right to left.

const int *ptr : ptr points to a integer that is const. Integer object can't be changed via ptr. For example check this code:

  1. #include<stdio.h>
  2. int main()
  3.   {  
  4.      const int *ptr;  
  5.      int a,b; 
  6.      a=10;  
  7.      b=20;  
  8.      ptr=&a;  
  9.      printf("%d\n",*ptr); 
  10.      //*ptr=20;    //Error
  11.      ptr=&b;  
  12.      printf("%d\n",*ptr); 
  13.      return 0;
  14.  }
If we uncomment the line 10 then there will be an error saying error: assignment of read-only location ‘*ptr’. This is because we are trying to modify the value pointed by ptr which is not allowed here. But we can modify the pointer to point to other integer object. In line 11 another integer is being assigned to ptr without an error.

int* const ptr:  ptr is a const pointer to a integer that means you can change the integer object via ptr but you can't change the pointer ptr itself. Check this example:
  1. #include<stdio.h>
  2. int main()
  3.   {
  4.     int a=10;
  5.     int b=20;
  6.     int *const ptr=&a;
  7.     printf("%d\n",*ptr);
  8.     //ptr=&b;     //Error
  9.     *ptr=30;
  10.     printf("%d\n",*ptr);
  11.     return 0;
  12.   }
Here the line 8 assigning another integer to the pointer ptr which is a const pointer, so this will generate an error if we uncomment it. Otherwise there is no problem in changing the value of the integer variable pointed by ptr. Another thing that you should keep in mind in the time of declaring such type of variable that you must set the pointer at the time of declaration as you can't do it later.

const int* const ptr: ptr is a const pointer to a const integer that means you can't change the pointer ptr itself and you can't change the integer object via ptr. Here is the example:
  1. #include<stdio.h>
  2. int main()
  3.   {
  4.      int a=10; 
  5.      int b=20; 
  6.      const int *const ptr=&a;  
  7.      printf("%d\n",*ptr);  
  8.      //ptr=&b;  //Error
  9.      //*ptr=30;  //Error
  10.      return 0;
  11.   }  
If we uncomment the lines 8 and 9 then there this code will generate errors. As in line 8 we are trying to modify a const pointer and in line 9 we are trying to  modify the value of a pointer pointing to a const object. As the pointer is also const then you need to initialize it at the time of declaration.
We also can use a handy rule to understand whether const applies to the pointer or the pointed data. split the statement at asteric(*) sign, then, if the const keyword appears in the left part (like in 'const int * ptr') - it belongs to pointed data, if it's in the right part ('int * const ptr') - it's about the pointer.

References: StackOverflow

No comments:

Post a Comment