Constant Pointers in C

Recently, I was working on a bug of Gnome-Logs. It involved some operations on pointers. More specifically, the pointer variables were used to determine the index of the newline character (\n) in a string and determine the substring between two newline characters (\n). I successfully submitted my patch solving the bug. But the reviewer mentioned that the following char pointers should be kept const.

gchar   *newline_index;
gchar   *prevpos;

I asked him why should they be const because we are modifying them on the following lines in the loop:

prevpos = newline_index + 1;
newline_index = strchr (prevpos, ‘\n’);

The reviewer then explained to me that the const modifier acts differently on pointers. Specifically, const modifier on a pointer allows us to change the memory location it is pointing to but does not allow us to change the value at that memory location. So, The above modifying code is perfectly valid even in the case of const pointer. I thank to the reviewer, David King, for clearing this misconception for me.

Here,  In this post I will explain about the different types of const pointers used in C:

There are basically three types of const pointers:

  1. Constant Pointers
  2. Pointer to Constant
  3. Constant Pointer to a Constant

Let us understand each of these one by one. We will first start with the Constant Pointers.


 

Constant Pointer

A constant pointer is a pointer that cannot change the address it is holding. We can also say that once a constant pointer starts pointing to a variable then it cannot point to any other variable. The syntax for const pointer is as follows:

<type of pointer>  * const <name of pointer>

An example declaration is:

char * const ptr;

Let us take a small program 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;
}

Here , we have two int variables var1 and var2 and and a constant pointer ptr.
we first initialize the constant pointer ptr variable to point to var1 and then to var2.

So, we assigned an address to a constant pointer and then we tried to change the address by assigning the address of some other variable to the same constant pointer.

If we compile the above program, we will get the following error:

$ gcc -Wall constptr.c -o constptr
constptr.c: In function ‘main’:
constptr.c:7: error: assignment of read-only variable ‘ptr’

We see that while compiling the compiler complains about ptr being a
read only variable. This means we cannot change the value ptr holds. Hence, a constant variable which points to some variable cannot be made to point to another variable.

Now, we discuss about the next type of const pointers: Pointer to Constant.


Pointer to Constant

A pointer through which one cannot change the value of variable it points to is known as a pointer to a constant. These type of pointers can change the address they point to but cannot change the value kept at those address. It was this type of constant pointer that I had misconception about while working on my patch.

The syntax for a pointer to a constant is given as:

const <type of pointer>* <name of pointer>

An example definition could be:

const char* ptr;

Let us understand the working of pointer to constant through a small program:

#include<stdio.h>

int main(void)
{
int var1 = 0;
const int* ptr = &var1;
*ptr = 1;
printf("%d\n", *ptr);

return 0;
}

In the above code, we defined a variable named var1 and initialized it to 0. Then we defined a pointer to constant ptr and intialize it to point to the address of var1. Next we change the value of var1 to 1 through the use of *ptr. Finally , we print the new value using printf.

When we compile the above code, we get the following error:

$ gcc -Wall constptr.c -o constptr
constptr.c: In function ‘main’:
constptr.c:7: error: assignment of read-only location ‘*ptr’

We see that the compiler complains about ‘*ptr’ being read only. It means that we cannot change the value using the ‘ptr’ variable as it is defined to be a pointer to a constant.

Next we will discuss the last type: Constant Pointer to a Constant


 

Constant Pointer to a Constant

This type is a mixture of the above two types. A constant pointer to a constant is a pointer that can neither change the address it is pointing to nor it can change the value present at that address.

The syntax for defining a constant pointer to a constant is:

const <type of pointer>* const <name of pointer>

An example definition can be given like:

const char* const ptr;

Let us understand the working of constant pointer to a constant through a small program:

#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 above program, we declared two integer variables var1 and var2 and initialize
them to 0. Next, we declared a constant pointer to a const ptr and intialized it to point to
var1. We then change the value and address of the pointer ptr in the next two lines. Finally, we print the value at the address pointed by ptr.

When we compile the above program, we get the following error:

$ 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 complains about both the value and address being changed.Hence, we infer that a constant pointer to a constant cannot change the address and value pointed by it.

I hope you have understood the concept of constant pointers in C and your misconceptions about it just like me must have been cleared through this post.

Looking forward to see you all, until then,

Happy Programming !!!!

 

 

 

 

Advertisements