Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Difference Between Constant Pointers and Pointer to Constants (http://www.go4expert.com/articles/difference-constant-pointers-pointer-t26816/)

poornaMoksha 30Sep2011 00:36

Difference Between Constant Pointers and Pointer to Constants
 
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!!!

jhon786 13Oct2011 11:57

Re: Difference Between Constant Pointers and Pointer to Constants
 
Thanks for telling this useful information and difference between Constant Pointers and Pointer to Constants.

bernaberna1 20Oct2011 18:08

Re: Difference Between Constant Pointers and Pointer to Constants
 
It's still sounds confusing but i'll get into it more in details. Thanks for posting.

poornaMoksha 23Nov2011 11:43

Re: Difference Between Constant Pointers and Pointer to Constants
 
Quote:

Originally Posted by jhon786 (Post 87930)
Thanks for telling this useful information and difference between Constant Pointers and Pointer to Constants.

You are welcome buddy!!!!

hakz_exp 17Feb2012 18:13

Re: Difference Between Constant Pointers and Pointer to Constants
 
Concepts are explained beautifully.Cemented in my mind :happy: , and the hack you mentioned wont work.

Scripting 18Feb2012 20:19

Re: Difference Between Constant Pointers and Pointer to Constants
 
Remarkable tutorial, and yes I think too that the hack won't work :D

lionaneesh 25Jun2012 15:53

Re: Difference Between Constant Pointers and Pointer to Constants
 
I just tried and it works, but I can't quite think of a reason. :-?


All times are GMT +5.5. The time now is 05:59.