≡ Menu

C Constant Pointers and Pointer to Constants Examples

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.

Add your comment

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

Comments on this entry are closed.

  • Jalal Hajigholamali June 8, 2012, 2:13 am

    Hi,

    Thanks, useful article

  • a magic June 8, 2012, 4:19 am

    thanks, good article~~~

  • bob June 8, 2012, 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

  • Umesh October 5, 2012, 6:53 am

    Thanks . Good article

  • ajeet October 20, 2012, 3:44 am

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

  • selvi November 7, 2012, 11:46 pm

    explained very well

  • mohammad November 10, 2012, 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]);

  • amol December 13, 2012, 2:09 am

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

  • Nabhirai jain March 5, 2013, 4:13 am

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

  • Csaba March 27, 2013, 1:30 am

    Thanks, clean and to the point article.

  • Ram April 2, 2013, 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.
    }

  • arpan April 22, 2013, 6:18 am

    thanks,very useful article (y)

  • ybc June 14, 2013, 7:10 am

    Very good explanation…perfect demonstration.

  • ashish September 2, 2013, 3:40 am

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

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

    Very good explanation…perfect demonstration.

  • Gerhard September 18, 2013, 9:24 am

    Excellent synopsis. Thanks for the help.

  • Hamilton Little October 19, 2013, 12:39 am

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

  • Javor Kamenov December 7, 2013, 5:47 am

    Very usefull !!!!!

  • Bharathi January 24, 2014, 12:56 am

    fabulous explanation with adequate examples

  • sivaram ch January 27, 2014, 6:48 am

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

  • Ajay March 21, 2014, 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.

  • Prashant March 23, 2014, 10:57 pm

    Nice explaination!!

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

    very nice explanation

  • Battu July 25, 2014, 2:19 am

    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

  • Manju July 29, 2014, 11:33 am

    Good explanation

  • murali November 5, 2014, 5:10 am

    after so long time i could understand clearly

  • Ram April 11, 2015, 7:28 am

    Great explanation given by author. Greatly appreciate that. Extremely helpful article

  • Naeem July 27, 2015, 12:10 am

    cleared pointers very well

  • Siddanagouda m October 8, 2015, 1:33 pm

    Pointer concept clearly explained here

  • sowmyashree November 1, 2015, 7:32 pm

    nice explaination..thanks..

  • ajay November 18, 2015, 6:24 am

    Hi,
    Great article…very clear concept on pointer..thanx

  • rehan shaikh January 17, 2016, 12:40 pm

    very helpful thanx …

  • Junaid January 27, 2016, 3:55 am

    after so long time i could understand clearly.

  • yogi March 8, 2016, 11:38 am

    veru nice article

  • Ramanan May 6, 2016, 1:23 am

    This is really nice article, thank u ,,,,

  • Om June 17, 2016, 8:33 pm

    Nice..

  • Baskaran B March 26, 2019, 11:57 am

    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 …. ?