C Constant Pointers and Pointer to Constants Examples

by Himanshu Arora on June 8, 2012

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.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 23 comments… read them below or add one }

1 Jalal Hajigholamali June 8, 2012 at 2:13 am

Hi,

Thanks, useful article

2 a magic June 8, 2012 at 4:19 am

thanks, good article~~~

3 bob June 8, 2012 at 1:01 pm

would be helpful if line numbers were shown on source code so we can match the compile error with the exact line without counting

4 Umesh October 5, 2012 at 6:53 am

Thanks . Good article

5 ajeet October 20, 2012 at 3:44 am

Your content was very helpful for me, it helped me so i like it

6 selvi November 7, 2012 at 11:46 pm

explained very well

7 mohammad November 10, 2012 at 6:51 am

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]);

8 amol December 13, 2012 at 2:09 am

hey dude this is just awsm….got my doubts cleared

9 Nabhirai jain March 5, 2013 at 4:13 am

Hi.
This is very good article on constant pointer. Examples and theory are properly
covered in brief.

10 Csaba March 27, 2013 at 1:30 am

Thanks, clean and to the point article.

11 Ram April 2, 2013 at 1:26 pm

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.
}

12 arpan April 22, 2013 at 6:18 am

thanks,very useful article (y)

13 ybc June 14, 2013 at 7:10 am

Very good explanation…perfect demonstration.

14 ashish September 2, 2013 at 3:40 am

explanation is simply to the point and most understandable than i found it given by others

15 nagarajan.k.r September 12, 2013 at 7:07 am

Very good explanation…perfect demonstration.

16 Gerhard September 18, 2013 at 9:24 am

Excellent synopsis. Thanks for the help.

17 Hamilton Little October 19, 2013 at 12:39 am

Absolutely fantastic synopsis. Thank you for the clear and precise explanation.

18 Javor Kamenov December 7, 2013 at 5:47 am

Very usefull !!!!!

19 Bharathi January 24, 2014 at 12:56 am

fabulous explanation with adequate examples

20 sivaram ch January 27, 2014 at 6:48 am

Thanks for explaining this toughest concept in a very easy way…

21 Ajay March 21, 2014 at 3:47 am

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.

22 Prashant March 23, 2014 at 10:57 pm

Nice explaination!!

23 kunwar singh June 17, 2014 at 10:23 am

very nice explanation

Leave a Comment

Previous post:

Next post: