In C language, a pointer is an object that stores the address of another object placed in the main memory.
A constant pointer is a pointer that contains an address that can not be altered.
Once the pointer is initialized with the address of an object, we can not change it to point to another object. However, we can still change the object's value to which the pointer points.
Below, we can see the syntax used to define a constant pointer object.
<dataType> *const <pointerName> = <address>;
We can define a constant pointer using an asterisk (*
) followed by the const
keyword. The three placeholders that are shown in the syntax are discussed below.
dataType
: This refers to the type of pointer we are using. The data type of the pointer should be the same as the object whose address it stores, for example, int, float, double, etc.
pointerName
: Here, we define the name of the pointer.
address
: Since we use a constant, we also have to initialize it with an address during declaration so that we will provide it with an address shown by the placeholder.
Now that we have understood what is a constant pointer and how it can be defined, we will now look at two types of cases that show us how constant pointers work.
In the code snippet below, we try to update the value (x
) to which the pointer points.
#include <stdio.h>#include <unistd.h>int main(){// we create an integer object xint x = 5;// we create an integer pointerint *const pointer = &x;// display the initial value of the pointerprintf("INITIAL VALUE!\n Value Inside Pointer: %p, Value At Address: %d\n\n", pointer, *pointer);// try to change the value to which the pointer points*pointer = 10;printf("AFTER VALUE UPDATE!\n Value Inside Pointer: %p, Value At Address: %d\n\n", pointer, *pointer);return 0;}
Line 7: We create an integer variable x
and initialize it to the value 5.
Line 9: We create a constant pointer called pointer
and initialize it with the address of x
.
Line 12: We will display the contents of the pointer.
Line 15: We try to change the value to which the pointer points by de-referencing it and assigning it the value 10
.
Line 16: We now print the modified contents of the pointer.
Above, we saw that we can see that we can alter the value that is contained in the address which is stored in the pointer.
Now, we will try to update the value inside the pointer to see if we can change the address that is stored inside the pointer.
#include <stdio.h>#include <unistd.h>int main(){// we create an integer object xint x = 5;// we create an integer pointerint *const pointer = &x;// display the initial value of the pointerprintf("INITIAL VALUE!\n Value Inside Pointer: %p, Value At Address: %d\n\n", pointer, *pointer);// now we try to change the address that is inside the pointerprintf("Trying To Change Value Inside Pointer!");int y = 100;pointer = &y;return 0;}
Note: When we run the program we see that we can not update the pointer to make it point to the variable
y
. This shows that the address of a constant pointer can not be updated.
Line 7: We create an integer variable x
and initialize it to the value 5
.
Line 9: We create a constant pointer called pointer
and initialize it with the address of x
.
Line 12: We display the contents of the pointer.
Line 16: We create a new variable y
and initialize it to 100
.
Line 17: We try to update the address stored in the pointer so it points to the object y
.
Now that we have defined constant pointers and how they work in C programs, let's try to solve the questions below.
Pointers quiz
What is a constant pointer?
A pointer that can not be modified.
A pointer that points to a constant value.
A pointer is initialized with a constant value.