Difference Between Constant Pointers and Pointer to Constants

Discussion in 'C' started by poornaMoksha, Sep 29, 2011.

  1. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    This article assumes that readers are aware of what pointers are and how they are used in 'C'. As a quick overview, A pointer is a special variable which holds the address of a variable of same type. For example :

    Lets say there is a variable 'int a=10' so,a pointer to variable 'a' can be defined as 'int* ptr = &a'.

    • '&' operator gives you the address of the variable on which this operator is applied
    • * operator is the value-of operator and fetches the value kept at address held by the variable it is applied on.
    For example, in the above example, *ptr will give the value of 'a' ie 10.

    Now, coming back to the topic. This articles tries to quickly explain the difference between two types of pointers :

    • Constant pointers
    • Pointer to Constants
    Let us take it one by one.

    1) Constant Pointers : These type of pointers are the one which cannot change address they are pointing to. This means that suppose there is a pointer which points to a variable (or stores the address of that variable). Now if we try to point the pointer to some other variable (or try to make the pointer store address of some other variable), then constant pointers are incapable of this.

    A constant pointer is declared as : 'int *const ptr' ( the location of 'const' make the pointer 'ptr' as constant pointer)

    2) Pointer to Constant : These type of pointers are the one which cannot change the value they are pointing to. This means they cannot change the value of the variable whose address they are holding.

    A pointer to a constant is declared as : 'const int *ptr' (the location of 'const' makes the pointer 'ptr' as a pointer to constant.

    Examples



    Lets look at some quick examples to understand the above two concepts more clearly :

    1) Constant pointers :
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int a[] = {10,11};
        int* const ptr = a;
    
        *ptr = 11;
    
        printf("\n value at ptr is  : [%d]\n",*ptr);
        printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);
    
        ptr++;
        printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);
    
        return 0;
    }
    Now, when we compile the above code, compiler complains :

    practice # gcc -Wall constant_pointer.c -o constant_pointer
    constant_pointer.c: In function ‘main’:
    constant_pointer.c:13: error: increment of read-only variable ‘ptr’​
    Hence we see very clearly above that compiler complains that we cannot changes the address held by a constant pointer.

    2) Pointer to Constants :
    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int a = 10;
        const int* ptr = &a;
    
    
        printf("\n value at ptr is  : [%d]\n",*ptr);
        printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);
    
        *ptr = 11;
    
        return 0;
    }
    Now, when the above code is compiled, the compiler complains :

    practice # gcc -Wall pointer_to_constant.c -o pointer_to_constant
    pointer_to_constant.c: In function ‘main’:
    pointer_to_constant.c:12: error: assignment of read-only location ‘*ptr’​
    Hence here too we see that compiler does not allow the pointer to a constant to change the value of the variable being pointed.

    A little hack



    Have you guyz heard about constant variables? Most of you might have, but for those who haven't :

    constant variables are the one whose values cannot be changed. For example:

    const int a = 10;

    The above line declares a constant variable 'a' with value 10. Now, since its a constant variable so any attempt to change the value of a will be disregarded by compiler. Lets try :

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        const int a = 10;
        a++;
    
        return 0;
    }
    The above program tries to change the value of constant variable 'a'. Lets see what the compiler has to say :

    practice # gcc -Wall hack_ptr.c -o hack_ptr
    hack_ptr.c: In function ‘main’:
    hack_ptr.c:6: error: increment of read-only variable ‘a’​
    We see that the compiler clearly says that we cannot change value of a read-only(const) variable 'a'.

    Now, ther interesting part. Can we have a mechanism through which we can change the value of 'a' without the compiler complaining???

    Hmmmmm...lets try pointers. Have a look at the following code :

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        const int a = 10;
        int* ptr = &a;
    
    
        printf("\n value at ptr is  : [%d]\n",*ptr);
        printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);
    
        *ptr = 11;
        printf("\n value at ptr is  : [%d]\n",*ptr);
    
        return 0;
    }
    
    Now, this is left as an exercise to the readers to see if the above program actually works?? If yes, then whats the reason??

    Conclusion



    To conclude, pointers are a powerful mechanism in 'C' to manipulate memory locations and values present at these locations. 'C' provides the concept of constant pointers and pointer to constants to have control on how pointers are being manipulated.

    Any one with questions or suggestions is most welcome!!!!!

    Stay tuned for more!!!
     
  2. jhon786

    jhon786 New Member

    Joined:
    Oct 12, 2011
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for telling this useful information and difference between Constant Pointers and Pointer to Constants.
     
  3. bernaberna1

    bernaberna1 New Member

    Joined:
    Oct 20, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    It's still sounds confusing but i'll get into it more in details. Thanks for posting.
     
  4. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    You are welcome buddy!!!!
     
  5. hakz_exp

    hakz_exp New Member

    Joined:
    Feb 17, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Concepts are explained beautifully.Cemented in my mind :happy: , and the hack you mentioned wont work.
     
  6. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    Remarkable tutorial, and yes I think too that the hack won't work :D
     
  7. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    I just tried and it works, but I can't quite think of a reason. :-?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice