Back to: C Tutorials For Beginners and Professionals
Pointer to Constant in C Language with Examples
In this article, I will discuss Pointer to Constant, Constant Pointer, and Constant Pointer to a Constant in C Language with Examples. Please read our previous articles discussing the Character Pointer in C Language with Examples. So, at the end of this article, you will understand the following pointers in detail.
- What is Pointer to Constant?
- What is a Constant Pointer?
- Constant Pointer to a Constant
- Difference between Pointer to Constant, Constant Pointer, and Constant Pointer to a Constant in C Language
What is Const in C?
The keyword const in C Programming Language can be applied to the declaration of any variable to specify that its value will not be changed. The most important point you must remember is that the keyword const applies to whatever is there immediately to its left. If there is nothing to its left, it applies to whatever is there immediately to its right. With this kept in mind, let us proceed and understand the Pointer to Constant, Constant Pointer, and Constant Pointer to a Constant in C Language with Examples.
Pointer to Constant in C Language:
In C, a pointer to a constant is a type of pointer that points to a value that should not be modified through the pointer. This concept is important for ensuring that certain data remains unchanged throughout the program, particularly when passing data to functions where you want to prevent accidental modifications.
The pointer will point to an address where it cannot change the value at that address. But it is possible that this pointer can point to some other addresses. Assigning values is not possible in the pointer to constant. The pointer value can be incremented or decremented. The pointer is pointing to the constant data objects.
Syntax: const <pointer-type> * <pointer-name>;
Example: const int *ptr;
In this declaration, ptr is a pointer to a const int. This means that ptr can point to an integer, but it cannot be used to change that integer’s value.
Program to Understand Pointer to Constant in C Language
In the below example, we have created two integer variables, i.e., a and b, with the values 10 and 20, respectively. Then, we created a pointer to the constant using the const keyword, i.e., const int *p; Once we created the constant pointer, we assigned the address of the variable a to this pointer. As it is a pointer to a constant, we cannot change the address. So, when we are trying to change the value of the pointer, i.e., *p = 5; it will give us an error. But it is possible to change the address of the pointer to a constant variable, i.e., p = &b;.
#include<stdio.h> int main () { int a = 10, b = 20; const int *p; p = &a; printf ("value of a is %d, *p is %d \n", a, *p); *p = 5; //wrong you can not change value at address printf ("value of a is %d, *p is %d \n", a, *p); p = &b; //you can change address return 0; }
Output:
Example: Pointer to a Constant
#include <stdio.h> int main() { int value = 10; const int *ptr = &value; printf("Value: %d\n", *ptr); // The following line will raise a compile-time error // *ptr = 20; value = 20; // OK: Changing the value directly is allowed printf("New Value: %d\n", *ptr); return 0; }
In this example, ptr points to value, but it cannot be used to change value. However, value can be changed directly, and ptr will still point to its new value.
Constant Pointer in C Language:
It’s also useful to distinguish a pointer to a constant from a constant pointer. A constant pointer must always point to the same memory location after it is assigned.
This concept is opposite to the Pointer to Constant concept. Here, the pointer will point to one address which we initialize at the declaration time. After that, we cannot change that address. But the value at that address we can change. We can assign a value to a constant pointer. Values cannot be incremented or decremented. Constant Pointer points to the data object.
Syntax: <pointer-type> *const <pointer-name> = <memory-address>;
Example: int *const ptr=&a;
In this case, ptr is a constant pointer to an int. This means that once ptr is assigned an address, it cannot point to a different address, but the value it points to can be changed.
Program to Understand Constant Pointer in C Language
In the below example, we have created two integer variables, i.e., a and b, with the values 10 and 20, respectively. Then we created a constant pointer using the const keyword, i.e., int *const p = &a; and we also assigned the address of the variable a at the time of this constant pointer declaration as it is a pointer constant, so we cannot change the address. So, when we are trying to change the address of the pointer, i.e., p = &b; it will give us an error. But it is possible to change the value of the constant pointer, i.e., *p = 5;
#include<stdio.h> int main () { int a = 10, b = 20; int *const p = &a; printf ("value of a is %d, *p is %d \n ", a, *p); *p = 5; //changing value at address printf ("value of a is %d, *p is %d \n ", a, *p); p = &b; //changing address wrong return 0; }
Output:
Here, you can see we are getting the error “assignment of read-only variable ‘p’“. It means that the value of the variable p, which p is holding, cannot be changed. In the above code, we are changing the value of ‘p’ from &a to &b, which is not possible with constant pointers. Therefore, we can say that the constant pointer, which points to some variable, cannot point to another variable.
Example: Constant Pointer
#include <stdio.h> int main() { int value1 = 10, value2 = 20; int *const ptr = &value1; *ptr = 30; // OK: Changing the value at the address is allowed // The following line will raise a compile-time error // ptr = &value2; printf("Value: %d\n", *ptr); return 0; }
In this example, once ptr is set to point to value1, it cannot be changed to point to value2, but the value of value1 can be changed via ptr.
Constant Pointer to a Constant in C Language:
A constant pointer to a constant is a pointer in C Programming Language, which is a combination of the above two pointers, i.e., Pointer to Constant and Constant Pointer. In this case, it is neither changing the address of the variable it is pointing to nor changing the value placed at this address.
Syntax: const <type of pointer> *const <name of the pointer>;
Example: const int *const ptr;
Here, ptr is a constant pointer to a const int. This means that you cannot change what ptr points to, nor can you change the value at the location it points to.
Let us understand Constant Pointer to a Constant in C Language with an Example. In the below example, We have declared two variables, i.e., a and b, with the values 10 and 20, respectively. Then we declare a constant pointer to a constant and then assign the address of the variable a. Then We try to change the value of the variable a through the pointer ptr and assign the address of variable b to this pointer ptr. Here, in both cases, we will get errors. And finally, we print the variable’s value, which is pointed by the pointer ‘ptr’.
#include <stdio.h> int main() { int a=10, b=20; const int* const ptr=&a; *ptr=12; //wrong ptr=&b; //wrong printf("Value of ptr is :%d",*ptr); return 0; }
Output:
Difference Between Pointer to Constant, Constant Pointer, and Constant Pointer to a Constant
In C programming, understanding the difference between a pointer to a constant, a constant pointer, and a constant pointer to a constant is crucial for managing how variables are accessed and modified. Let us understand the differences between a Pointer to a Constant (const type *ptr), a Constant Pointer (type *const ptr), and a Constant Pointer to a Constant (const type *const ptr) in C Language.
Pointer to Constant (const int *ptr) :
This is a pointer to a constant integer. In this case, we cannot change the value which is pointed by the pointer ptr, but you can change the pointer itself, i.e., you can change the address which is held by the pointer. So, here, the pointer value is constant, but the address can be changed.
#include<stdio.h> #include<stdlib.h> int main() { int a =10, b =20; const int *ptr = &a; //The following is illegal statement (assignment of read-only location *ptr) //*ptr = b; printf( "value pointed to by ptr: %d\n", *ptr); // ptr address can be changed and is legal ptr = &b; printf( "value pointed to by ptr: %d\n", *ptr); }
Output:
There is no difference between const int *ptr and int const *ptr as both are pointers to a const integer, and the position of *(asterisk) is also the same. So, the following program will give you the same output as the previous one.
int main() { int a =10, b =20; int const *ptr = &a; //The following is illegal statement (assignment of read-only location *ptr) //*ptr = b; printf( "value pointed to by ptr: %d\n", *ptr); // ptr address can be changed and is legal ptr = &b; printf( "value pointed to by ptr: %d\n", *ptr); }
Characteristic: You cannot change the value pointed to by the pointer, but you can change the pointer itself to point to another value.
const int *ptr; int value1 = 10, value2 = 20; ptr = &value1; // Allowed // *ptr = 30; // Compile-time error: cannot modify the value pointed to ptr = &value2; // Allowed: can change the pointer to point to another address
Constant Pointer (int *const ptr):
This is a constant pointer to a non-constant integer. In this case, you cannot change the pointer ptr, but you can change the value pointed by the pointer ptr. So, here, the pointer address is constant, but the value can be changed.
#include<stdio.h> #include<stdlib.h> int main() { int a =10, b =20; int *const ptr = &a; printf( "Value pointed to by ptr: %d\n", *ptr); printf( "Address ptr is pointing to: %d\n", ptr); //The following statement is illegal (assignment of read-only variable ptr) //ptr = &b; // changing the value at the address ptr is pointing to *ptr = b; printf( "Value pointed to by ptr: %d\n", *ptr); printf( "Address ptr is pointing to: %d\n", ptr); }
Output:
Characteristic: You can change the value pointed to by the pointer, but you cannot change the pointer to point to another value.
int value1 = 10, value2 = 20; int *const ptr = &value1; *ptr = 30; // Allowed: can modify the value pointed to // ptr = &value2; // Compile-time error: cannot change the pointer itself
Constant Pointer to a Constant (const int * const ptr):
This is a constant pointer to a constant integer. You can neither change the value pointed by ptr nor the address of the pointer ptr. So, here, both pointer address and value are constant.
#include<stdio.h> #include<stdlib.h> int main() { int a =10, b =20; int char *const ptr = &a; printf( "Value pointed to by ptr: %c\n", *ptr); printf( "Address ptr is pointing to: %d\n\n", ptr); //The following two statements are illegal // ptr = &b; illegal statement (assignment of read-only variable ptr) // *ptr = b; illegal statement (assignment of read-only location *ptr) }
Characteristic: Neither the value pointed to by the pointer nor the pointer itself can be changed.
int value = 10; const int *const ptr = &value; // *ptr = 20; // Compile-time error: cannot modify the value pointed to // ptr = &value2; // Compile-time error: cannot change the pointer itself
Key Differences:
- Pointer to a Constant (const type *ptr): Used when you want to prevent modification of the value pointed to by the pointer. This is useful when you want to protect the data the pointer refers to but still need the flexibility to point to different data.
- Constant Pointer (type *const ptr): Used when you need a pointer to always point to the same memory location. The data at that location can be modified, but the pointer cannot be redirected to another location.
- Constant Pointer to a Constant (const type *const ptr): Combines both restrictions. The pointer always points to the same location, and the data at that location cannot be modified through the pointer. This is useful for protecting the pointer and the data it points to.
In the next article, I will discuss Null Pointer in C Language with Examples. In this article, I try to explain Pointer to Constant in C Language with Examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.