Pointer to a constant is a powerful aspect of C. The pointer to a constant can protect users of a function from modification of a parameter by the function.
Pointers to a constant
We can define a pointer to point to a constant. If we do so, the pointer can’t be used to modify the value it is referencing to. Below we declare integer num and constant integer limit. Afterwards we declare pointer to an integer and a pointer to an integer constant and we initialize all the variable immediately.
#include <stdio.h>
int main(void)
{
int num;
const int limit = 100;
int *ptr;
const int *cptr;
num = 10;
ptr = #
cptr = &limit;
printf("Adress of num%p\n", &num);
printf("Adress of limit%p\n", &limit);
printf("Adress of ptr%p\n", &ptr);
printf("Adress of cptr%p\n", &cptr);
return (0);
}
Now… What do you think that will happen if we try to change the variables num and limit as below?
*ptr = 9;
*cptr = 99;
After compiling...:
Terminal> gcc ConstantsAndPointers.c
ConstantsAndPointers.c: In function ‘main’:
ConstantsAndPointers.c:18:15: error: assignment of read-only location ‘*cptr’
18 | *cptr = 99;
| ^
It is fine to dereference a constant pointer to simply read the integer’s value like that:
printf("Value of cptr: %d\n", *cptr);
But it is not fine to change the value as you can see in the error above. More examples what we can do or not:
cptr = # //we can change the pointer to reference another integer
*cptr = 150; //we can't assign to a variable that is constant
- cptr can be assigned to point to different constant and non constant integers,
- cptr can be dereferenced to read the value of a variable,
- cptr can’t be dereferenced to change the variable it points to.
Constant pointers to non constants
int num;
int *const constptr = #
As seen above, we declared constant pointer and we immediately assigned it to a non constant variable. This means that the pointer can’t be changed but the data the pointer points to can be changed.
- constptr must be initialized to non constant variable,
- constrptr can’t be modified,
- The data that constrptr points to can be modified.
#include <stdio.h>
int main(void)
{
int num;
int *const constptr = #
num = 10;
printf("Address of constptr: %p\n", &constptr);
printf("Value of constptr and num: %d, %d\n", *constptr, num);
*constptr = 9;
printf("Address of constptr: %p\n", &constptr);
printf("Value of constptr and num: %d, %d\n", *constptr, num);
return (0);
}
Terminal> gcc ConstantPointerToNonConstant.c
Terminal> ./a.out
Address of constptr: 0x7ffd4f6a5d60
Value of constptr and num: 10, 10
Address of constptr: 0x7ffd4f6a5d60
Value of constptr and num: 9, 9
In the example above we can see how the data that constant pointer points to can be changed.
Constant pointer to constants
Constant pointer to constants is not so frequently used pointer type. In this case, the pointer can’t be changed and either the data it points to. You can see the example of implementation below:
const int limit = 100;
const int *const constptr = &limit;
Pointers to constants can have also multiple levels of indirection.
Pointer Type | Pointer Modifiable | Data Pointed to Modifiable |
---|---|---|
Pointer to a non constant | YES | YES |
Pointer to a constant | YES | NO |
Constant pointer to a non constant | NO | YES |
Constant pointer to a constant | NO | NO |
SOURCES:
[1] Understanding and Using C Pointers by Richard Reese (O’Reilly). Copyright 2013 Richard Reese, Ph.D. 978-1-449-34418-4