Constant Pointers
In C language, a constant pointer refers to a pointer whose address cannot be changed, meaning the address will remain unchanged. Therefore, we can say that if a constant pointer points to a certain variable, it cannot point to other variables.
Syntax of Constant Pointers
<pointer type> * const <pointer name>;
The declaration of a constant pointer is as follows:
int * const ptr;
Understanding Constant Pointers through Examples
#include <stdio.h>
int main(){int a = 1;int b = 2;int * const ptr; ptr = &a;ptr = &b; printf("ptr的值为:%d", *ptr); return 0;}
The explanation of the above code is as follows:
-
We declared two variables a and b, with values 1 and 2 respectively.
-
We declared a constant pointer.
-
First, we assigned the address of variable ‘a’ to pointer ‘ptr’.
-
Then, we tried to assign the address of variable ‘b’ to pointer ‘ptr’.
-
Finally, we attempted to print the value of the variable pointed to by ‘ptr’.
👇点击领取👇
👉C语言知识资料合集
Output Result

From the above output, it can be seen that the code produced an error: “assignment of read-only variable ‘ptr'”. This means that the value held by pointer ‘ptr’ is read-only. In the above code, we attempted to change the value of ‘ptr’ from &a to &b, but this is not possible when using a constant pointer. Therefore, we can say that a constant pointer pointing to a certain variable cannot point to other variables.
Pointer to Constant
A pointer to a constant is a pointer through which the value of the variable it points to cannot be changed. The address of these pointers can be changed, but the value of the variable pointed to cannot be changed.
Syntax of Pointer to Constant
const <pointer type> * <pointer name>
The declaration of a pointer to a constant is as follows:
const int *ptr;
Understanding through Examples.
-
First, we write a code example that changes the pointer value:
#include <stdio.h>
int main(){int a = 100;int b = 200;const int *ptr; ptr = &a;ptr = &b; printf("ptr的值为:%u", ptr); return 0;}
The explanation of the above code is as follows:
-
We declared two variables a and b, with values 100 and 200 respectively.
-
We declared a pointer to a constant.
-
First, we assigned the address of variable ‘a’ to pointer ‘ptr’.
-
Then, we assigned the address of variable ‘b’ to pointer ‘ptr’.
-
Finally, we attempted to print the value of ‘ptr’.
Output Result

The above code runs successfully and displays the value of ‘ptr’ in the output.
-
Now, we write a code example that changes the value of the variable pointed to by the pointer.
#include <stdio.h>
int main(){int a = 100;int b = 200;const int *ptr; ptr = &b;*ptr = 300; printf("ptr的值为:%d", *ptr); return 0;}
The explanation of the above code is as follows:
-
We declared two variables ‘a’ and ‘b’, with values 100 and 200 respectively.
-
We declared a pointer to a constant.
-
We assigned the address of variable ‘b’ to pointer ‘ptr’.
-
Then, we attempted to modify the value of variable ‘b’ through pointer ‘ptr’.
-
Finally, we tried to print the value of the variable pointed to by ‘ptr’.
Output Result

The above code shows an error: “assignment of read-only location ‘*ptr'”. This error means that we cannot change the value of the variable pointed to by the pointer.
Constant Pointer to Constant
A constant pointer to a constant refers to the combination of the above two types of pointers. It can neither change the address of the pointed variable nor change the value at that address.
Syntax
const <pointer type> * const <pointer name>;
The declaration of a constant pointer to a constant is as follows:
const int * const ptr;
Understanding through Examples.
#include <stdio.h>
int main(){int a = 10;int b = 90;const int * const ptr = &a; *ptr = 12;ptr = &b; printf("ptr的值为:%d", *ptr); return 0;}
The explanation of the above code is as follows:
-
We declared two variables ‘a’ and ‘b’, with values 10 and 90 respectively.
-
We declared a constant pointer to a constant and assigned the address of ‘a’ to pointer ‘ptr’.
-
We attempted to change the value of variable ‘a’ through pointer ‘ptr’.
-
Then, we tried to assign the address of variable ‘b’ to pointer ‘ptr’.
-
Finally, we printed the value of the variable pointed to by ‘ptr’.
Output Result

The above code shows errors: “assignment of read-only location ‘*ptr'” and “assignment of read-only variable ‘ptr'”. Therefore, we conclude that a constant pointer to a constant can neither change the address nor change the value of the variable it points to.
Programmer Technical Group
Join the group and remember to note: city, nickname, and technical direction.