Overview
for beginners, pointers are always a very difficult concept to understand in C language. In this article, we'll explain the difference between constant pointers, pointer constants, const pointer to const (PS: The landlord thinks this can be translated to constant pointers to constants)
Constant pointer
Let's first understand what a constant pointer is. A constant pointer refers to the address that the pointer is a constant. In other words, once the constant pointer points to a variable, you can't let the constant pointer point to another variable.
The declaration method for a constant pointer is as follows:
<type of pointer> * Const <name of pointer>
Example of a constant pointer sound:
Let's use a small piece of code to illustrate the constant pointer:
#include <stdio.h>
int main (void)
{
int var1 = 0, var2 = 0;
int * Const PTR = &var1;
ptr = &var2;
printf ("%d\n", *ptr);
return 0;
}
In the example above:
- We've defined two variables var1 and var2.
- Declares a constant pointer ptr and points it to the VAR1
- Then we let PTR point to var2.
- Finally, we're trying to print what this pointer points to.
In short, we defined a constant pointer and tried to modify the address that the constant pointer points to
Now, let's compile this program:
So, once the constant pointer points to an address, we can't change the address of the constant pointer (PS: But you can modify what the constant pointer points to)
Pointer constants
It's obvious from the name that a pointer, we can't modify what the pointer is pointing to, is called a pointer constant. For such pointers, you can modify the address that the pointer points to, but you cannot modify what the pointer points to
The pointer constants are defined as follows:
Const <type of pointer> * <name of pointer>
Or
<type of pointer> Const * <name of Pointer>
Examples of pointer constants are as follows:
const int *PTR;
int const *PTR;
Let's use a small piece of code to explain the pointer constants:
#include <stdio.h>
int main (void)
{
int var1 = 0;
const int *PTR = &var1;
*ptr = 2;
printf ("%d\n", *ptr);
return 0;
}
In the code above:
- We define a variable var1 and assign it to a value of 0.
- We define a pointer constant and let it point to the variable var1
- We tried to modify the value pointed to by the pointer constant
- Print the value pointed to by the pointer constant
Then we compile the above program:
We can see that *ptr is a read-only property. This means that if the pointer constant points to a variable, we cannot modify the value that the pointer constant points to
Constant pointer to constant
If you understand the two pointer types above, you should also be very well understood as a mixture of the two pointers. A constant pointer to a constant that you can neither modify the address of the pointer nor modify what the pointer points to
Constant pointers to constants are defined as follows:
Const <TYPE of pointer> * Const <name of pointer>
Example:
Let's write a short piece of code to understand the constant pointer to a constant:
#include <stdio.h>
int main (void)
{
int var1 = 0, var2 = 0;
const int * Const PTR = &var1;
*ptr = 1;
ptr = &var2;
printf ("%d\n", *ptr);
return 0;
}
In the code above:
- We've declared two variables var1 and var2.
- We declare a constant pointer ptr to a constant and let the PTR point to the variable var1
- We tried to modify the pointer's address and what the pointer points to.
When we compile this code:
The most common use of the combination of pointers and constants is to take the form of functions as parameters to ensure the immutable nature of the arguments in the modulated function, what is the difference between constant pointers and pointer constants?
Summarize
Okay, now to summarize the difference between constant and pointer constants
First of all must understand which definition is a constant pointer, which is a pointer constant, here can remember three words to deepen the memory:
* (pointers) and const (constants) who read first before; * Symbolizes the address, the const symbolizes the content, who is in the front who is not allowed to change.
Well, let's take a look at this example:
int a =3;
int b = 1;
int c = 2;
int const *P1 = &b;//const before, defined as constant pointer
int *const p2 = &c;//* before, defined as pointer constants
Constant pointer p1: The address you point to can change, but the content cannot be assignable, and changes to the content can only be changed by modifying the address.
- P1 = &a is correct, but *p1 = A is wrong.
- Pointer constant P2: The address that you point to cannot be assigned a value, but the content can be changed and must be initialized for the address to follow a lifetime.
- P2= &a is wrong, and *p2 = A is correct.
- For a constant pointer P1, we can change the address it points to, but we can't change the point, and if we change it, we get an error, and the following is a compiler-prompted error after 18 lines of code uncomment:
The following are the results of the debugging in the Vim editor
The above code can be correctly output after commenting 18 lines and 24 lines of code, the following figure is the correct result
The output can be seen, for constant pointer P1, change its address point, the content also changes with the address change.
And for the pointer constant P2, the address is fixed after initialization, and the content can be assigned to the value at any time.
For a constant pointer P1, we can change the address it points to, but we can't change the point, and if we change it, we get an error, and the following is a compiler-prompted error after 18 lines of code uncomment:
The output can be seen, for constant pointer P1, change its address point, the content also changes with the address change.
And for the pointer constant P2, the address is fixed after initialization, and the content can be assigned to the value at any time.
For a constant pointer P1, we can change the address it points to, but we can't change the point, and if we change it, we get an error, and the following is a compiler-prompted error after 18 lines of code uncomment:
After the introduction, I think you should know the difference between the constant pointer and the pointer constant.