Pointers in C has always been a complex concept to understand for newbies. In this article, we will explain the difference between constant pointer, pointer to constant and constant pointer to constant.
This article is part of the ongoing series on C pointers: part 1, part 2, part 3 (this article)
Constant Pointers
Lets first understand what a constant pointer is. A constant pointer is a pointer that cannot change the address its holding. In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable.
A constant pointer is declared as follows :
<type of pointer> * const <name of pointer>
An example declaration would look like :
int * const ptr;
Lets take a small code to illustrate these type of pointers :
#include<stdio.h> int main(void) { int var1 = 0, var2 = 0; int *const ptr = &var1; ptr = &var2; printf("%d\n", *ptr); return 0; }
In the above example :
- We declared two variables var1 and var2
- A constant pointer ‘ptr’ was declared and made to point var1
- Next, ptr is made to point var2.
- Finally, we try to print the value ptr is pointing to.
So, in a nutshell, we assigned an address to a constant pointer and then tried to change the address by assigning the address of some other variable to the same constant pointer.
Lets now compile the program :
$ gcc -Wall constptr.c -o constptr constptr.c: In function ‘main’: constptr.c:7: error: assignment of read-only variable ‘ptr’
So we see that while compiling the compiler complains about ‘ptr’ being a read only variable. This means that we cannot change the value ptr holds. Hence we conclude that a constant pointer which points to a variable cannot be made to point to any other variable.
Pointer to Constant
As evident from the name, a pointer through which one cannot change the value of variable it points is known as a pointer to constant. These type of pointers can change the address they point to but cannot change the value kept at those address.
A pointer to constant is defined as :
const <type of pointer>* <name of pointer>
An example of definition could be :
const int* ptr;
Lets take a small code to illustrate a pointer to a constant :
#include<stdio.h> int main(void) { int var1 = 0; const int* ptr = &var1; *ptr = 1; printf("%d\n", *ptr); return 0; }
In the code above :
- We defined a variable var1 with value 0
- we defined a pointer to a constant which points to variable var1
- Now, through this pointer we tried to change the value of var1
- Used printf to print the new value.
Now, when the above program is compiled :
$ gcc -Wall constptr.c -o constptr constptr.c: In function ‘main’: constptr.c:7: error: assignment of read-only location ‘*ptr’
So we see that the compiler complains about ‘*ptr’ being read-only. This means that we cannot change the value using pointer ‘ptr’ since it is defined a pointer to a constant.
Constant Pointer to a Constant
If you have understood the above two types then this one is very easy to understand as its a mixture of the above two types of pointers. A constant pointer to constant is a pointer that can neither change the address its pointing to and nor it can change the value kept at that address.
A constant pointer to constant is defined as :
const <type of pointer>* const <name of pointer>
for example :
const int* const ptr;
Lets look at a piece of code to understand this :
#include<stdio.h> int main(void) { int var1 = 0,var2 = 0; const int* const ptr = &var1; *ptr = 1; ptr = &var2; printf("%d\n", *ptr); return 0; }
In the code above :
- We declared two variables var1 and var2.
- We declared a constant pointer to a constant and made it to point to var1
- Now in the next two lines we tried to change the address and value pointed by the pointer.
When the code was compiled :
$ gcc -Wall constptr.c -o constptr constptr.c: In function ‘main’: constptr.c:7: error: assignment of read-only location ‘*ptr’ constptr.c:8: error: assignment of read-only variable ‘ptr’
So we see that the compiler complained about both the value and address being changed. Hence we conclude that a constant pointer to a constant cannot change the address and value pointed by it.
Comments on this entry are closed.
Hi,
Thanks, useful article
thanks, good article~~~
would be helpful if line numbers were shown on source code so we can match the compile error with the exact line without counting
Thanks . Good article
Your content was very helpful for me, it helped me so i like it
explained very well
very good,
but my problem is : can we point to a constant in c ?
something like this:
const char A[10]=…..;
char *A_pointer=&A[0];
for (i=0;i=9;i++)
printf(“%u”,*A_pointer[i]);
hey dude this is just awsm….got my doubts cleared
Hi.
This is very good article on constant pointer. Examples and theory are properly
covered in brief.
Thanks, clean and to the point article.
int main()
{
const char*=””;
char str[]=”Hello”;
s=str;
now if i write str=”welcome” the compiler give me an error which is obvious as s is pointing to the same string and it is constant but if write
str[0]=’z’;
the compiler do not gives any error and the program runs successfully, why? I am not getting.
}
thanks,very useful article (y)
Very good explanation…perfect demonstration.
explanation is simply to the point and most understandable than i found it given by others
Very good explanation…perfect demonstration.
Excellent synopsis. Thanks for the help.
Absolutely fantastic synopsis. Thank you for the clear and precise explanation.
Very usefull !!!!!
fabulous explanation with adequate examples
Thanks for explaining this toughest concept in a very easy way…
Answer to ram,
it is because when u are doing str=”welcome” , it means u are trying to change the address of str (nothing but &str[0]) which is assigned by the compiler and it is a constant pointer.
but when u are doing str[0]=’Z’ , u are trying to change the value at str[0] which is allowed.
Nice explaination!!
very nice explanation
Constant pointer : int *const p
Pointer to a constant: int const *p or const int *p
Constant pointer to a constant: const int *const p
Good explanation
after so long time i could understand clearly
Great explanation given by author. Greatly appreciate that. Extremely helpful article
cleared pointers very well
Pointer concept clearly explained here
nice explaination..thanks..
Hi,
Great article…very clear concept on pointer..thanx
very helpful thanx …
after so long time i could understand clearly.
veru nice article
This is really nice article, thank u ,,,,
Nice..
Very good explanation. Clear all of my doubts. But still I need one thing. Can u give some real time example for pointer to a constant or constant pointer. And also constant pointer that points to constant …. ?