“From today on, study hard and make progress every day”
Repetition is the best method for memory; spend one minute every day to remember the basics of C language.
“Series of 100 Essential Notes for C Language Beginners“
Persevere! We are finally entering the core content, which is also the most important and difficult part of C language.
Keep it up! Youngsters!
[~~ I am a beginner, please forgive any mistakes ~~]
52. Which Pointer is the ‘Constant’? const int* vs int* const in C Language
Introduction
Imagine a pointer as an envelope, which has two important parts:
- • The envelope itself (pointer variable)
- • The contents inside the envelope (the memory address the pointer points to)
The key to understanding <span>const int*</span> and <span>int* const</span> lies in:<span>const</span> protecting either the contents of the envelope or the envelope itself?
First Type: Pointer to a Constant <span>const int*</span>
Basic syntax:
const int* ptr; // Pointer to a constant
Specific usage:
int value = 10;
const int* ptr = &value; // Points to an integer constant
*ptr = 20; // Error! Cannot modify the value of the pointed variable through ptr
value = 20; // Allowed! Directly modifying the original variable is fine
int another = 30;
ptr = &another; // Allowed! The pointer itself can point to another address
Core Feature: The pointer can change its target, but cannot modify the data through the pointer. Similar to an envelope that can hold other letters, but the content of the original letter cannot be changed.
Second Type: Constant Pointer <span>int* const</span>
int* const ptr; // Constant pointer
Specific usage:
int value = 10;
int another = 20;
int* const ptr = &value; // The pointer itself is constant
*ptr = 30; // Allowed! Can modify the value through the pointer
ptr = &another; // Error! The pointer itself cannot point to another address
value = 40; // Allowed! Directly modifying the original variable
Core Feature: The pointer is fixed to one address, but can modify the data at that address through the pointer. Similar to an envelope that can only hold the original letter, but you can change the content of the letter.
Combination: Constant Pointer to a Constant
Basic syntax:
const int* const ptr; // Constant pointer to a constant
Specific usage: Neither the envelope nor the content of the letter can be changed.
int value = 10;
const int* const ptr = &value; // Double protection
*ptr = 20; // Error! Cannot modify the pointed value
ptr = &another; // Error! Cannot change the pointed address
Summary
- •
<span>const int* ptr</span>→ “ptr is a pointer to an int constant” - •
<span>int* const ptr</span>→ “ptr is a constant pointer to an int”
The true “constant pointer” is<span>int* const</span>!
Some students contacted me, wanting to have a study exchange group. I hesitated to create one before due to concerns about advertisements, but considering it would be convenient, I will try to set up a group this time.
If you need it, hurry up and join, valid for 7 days.

———- End ———-
[Special Statement: All articles in this public account are original or authorized by the author, some content and images are sourced from the internet, please feel free to use them. The views are for learning reference only. If there are any mistakes, please forgive me~~]


“If you like C, please like it”
Click the bottom right corner to see more
“