Pointers are the most powerful as well as complex component of C programming. For newbies, it’s like learning rocket science in C. However, I have tried my best to simplify things.
In this ongoing series of C programming tutorial, I have explained many concepts related to pointers. Here in this section we will focus on some confusing pointer terminologies. We will learn and compare constant pointer with pointer to constant and constant pointer to constant.
What is constant pointer?
To understand a constant pointer, let us recall definition of a constant variable. Constant variable is a variable whose value cannot be altered throughout the program.
Similarly, constant pointer is a pointer variable whose value cannot be altered throughout the program. It does not allows modification of its value, however you can modify the value pointed by a pointer.
In other words, constant pointer is a pointer that can only point to single object throughout the program.
Syntax to declare constant pointer
<pointer-type> * const <pointer-name> = <memory-address>;
Note: You must initialize a constant pointer at the time of its declaration.
Example to declare constant pointer
int num;
int * const constant_pointer = # // Constant pointer to num
Note: We use
const
keyword to declare a constant pointer.
The compiler will generate compilation error on failure of any of the two conditions.
- You must initialize a constant pointer during its declaration.
- A constant pointer must not be re-assigned.
Example program to use constant pointer
Let us write an example program to demonstrate constant pointer in C.
/**
* C program to demonstrate constant pointer
*/
#include <stdio.h>
int main()
{
int num1, num2;
// Constant pointer to num1
int * const const_ptr = &num1;
// Assign 10 to num1 using pointer
// Modification of value pointed by pointer is allowed
*const_ptr = 10;
// Re-assignment of constant pointer
// Modification of pointer value is not allowed
const_ptr = &num2; // Error
printf("Num1 = %d\n", num1);
printf("Num1 = %d\n", *const_ptr);
return 0;
}
Let us dry run the above program.
- First, we declared two integer variable num1, num2 and an integer constant pointer const_ptr that points to num1.
- The statement
*const_ptr = 10;
assigns 10 to num1. - Next we tried re-assignment of constant pointer i.e.
const_ptr = &num2;
. The statement will generate compilation error, since a constant pointer can only point to single object throughout the program. - The last two
printf()
statements are used to test value of num1.
On compilation it generates following error message.
constantpointer.c: In function 'main':
constantpointer.c:16:15: error: assignment of read-only variable 'const_ptr'
const_ptr = &num2; // Error
^
What is pointer to constant?
Pointer to constant is a pointer that restricts modification of value pointed by the pointer. You can modify pointer value, but you cannot modify the value pointed by pointer.
Note: There is a minor difference between constant pointer and pointer to constant. A constant pointer can only point to single object throughout the program. You can, alter the value pointed by pointer, but cannot alter pointer value.
Whereas pointer to a constant cannot modify the value pointed by pointer, but can alter its value.
Syntax to declare pointer to constant
const <pointer-type> * <pointer-name>;
Example to declare pointer to constant
int num = 10;
const int * ptr_to_const = # // Pointer to constant
Example program to use pointer to constant
Let us demonstrate pointer to a constant using an example.
/**
* C program to demonstrate pointer to constant
*/
#include <stdio.h>
int main()
{
int num = 10;
// Pointer to constant
const int * ptr_const;
// Modification of pointer value is allowed
ptr_const = # // ptr_const points to num
// Direct modification on num is allowed
num = 20;
// Assign value to num
// Modification of value pointed by pointer is not allowed
*ptr_const = 100; // Error
printf("Num = %d\n", num);
printf("Num = %d\n", *ptr_const);
return 0;
}
The above program generates compilation error, since we tried to assign value to read only memory location (i.e. the value pointed by the pointer). Below is the compilation error generated by *ptr_const = 100;
pointerconstant.c: In function 'main':
pointerconstant.c:16:16: error: assignment of read-only location '*ptr_const'
*ptr_const = 100; // Error
^
Note: Pointer to constant restricts modification of value pointed by the pointer. However, do not think that C compiler converts variable pointed by pointer as constant variable.
Pointer to constant does not allows you to modify the pointed value, using pointer. However, you can directly perform modification on variable (without using pointer).
For example,
int num = 10;
const int * ptr = #
// Modification of pointed value
// using pointer is not allowed
*ptr = 20; // Error
// You can still modify the pointed value directly
num = 20; // Works
What is constant pointer to constant?
A constant pointer to constant is a combination of constant pointer and pointer to constant. It is a pointer that does not allow modification of pointer value as well as value pointed by the pointer.
Syntax to declare constant pointer to constant
const <pointer-type> * const <pointer-name> = <memory-address>;
Example to declare constant pointer to constant
int num = 10;
// Constant pointer to a constant
const int * const ptr = #
Example program to use constant pointer to constant
Let us demonstrate the concept of constant pointer to constant in C program.
/**
* C program to demonstrate constant pointer to constant
*/
#include <stdio.h>
int main()
{
int num1 = 10;
int num2 = 20;
// Declare constant pointer pointing at num1
const int * const ptr = &num1;
// Modification of constant pointer value is not allowed
// Re-assignment of memory address to ptr
ptr = &num2; // Error
// Modification of value pointed by pointer is also not allowed
// Re-assignment of value to ptr
*ptr = 100; // Error
printf("Num1 = %d\n", num1);
printf("Num1 = %d\n", *ptr);
return 0;
}
The above program generates two compilation error. First in the statement ptr = &num2;
since we tried to assign value to a constant pointer. Second in the statement *ptr = 100;
since we tried to assign value pointed by a pointer to constant.
constantpointerconstant.c: In function 'main':
constantpointerconstant.c:14:9: error: assignment of read-only variable 'ptr'
ptr = &num2; // Error
^
constantpointerconstant.c:17:10: error: assignment of read-only location '*ptr'
*ptr = 100; // Error
^
Difference between constant pointer, pointer to constant and constant pointer to constant
Okay so got basic picture of a constant pointer, pointer to constant and constant pointer to constant. Let us summarize the things for quick recap.
Constant pointer | Pointer to constant | Constant pointer to constant |
---|---|---|
Pointer value cannot be modified | Pointer value can be modified | Pointer value cannot be modified |
Value pointed by the pointer can be modified | Value pointed by the pointer cannot be modified | Value pointed by the pointer cannot be modified |