1, What is const?
Const is a key word of C language (ANSI C), which plays an important role. It defines that a variable is not allowed to be changed and has a static effect. Using const can improve the security and reliability of the program to a certain extent. In addition, when watching other people's code, a clear understanding of the role of const is also helpful to understand each other's program.
We call const a constant modifier, which means that if a variable is modified by const, it does not have the attribute of the variable, but has a certain constant attribute, which can not be modified like a constant.
Why don't we say absolute here, which means that the content it modifies is a constant? Look at the picture below. We know that when defining an array (not C99), variables cannot be used to define the size of the array, but constants. If const modifies a constant, the compiler will not report an error. Therefore, the content of const modification still retains the properties of some variables. We can say that const is a constant variable, and the modified variable can be called a constant variable
2, Some usage of const
1.const modifier variable
After const modifies the n variable, the n can not be changed. We can find that the n can not be modified as an lvalue after checking the error.
There is also a wrong way to write: note that const must be initialized when modifying variables
Here is the correct way to write:
const int a = 10;
Of course, when modifying variables, changing the position of const has no effect. The position exchange of const and int is the same as above
int const a = 10;
2.const modifier pointer
At this time, we need to consider the positional relationship among const, int and *.
const int *p; int const *p ; int *const p; const int *const p
const int *p
const is located to the left of *, so it modifies * p, that is, * p cannot be changed, that is, the data of the address pointed to by p is a constant.
The following is an example:
Error example (want to change * p)
int main() { int n = 0; const int* p = &n; *p = 20; return 0; }
Correct example: the content that can be changed should be p rather than * P
int main() { int n = 0; const int* p = &n; //Define a new variable int m = 100; p = &m; printf("%d", *p);//We changed the value of * p by changing the address pointed to by p return 0; }
int const *p
The analysis here is similar to the one above. const is on the left of *, so it modifies * p, that is, * p cannot be changed, that is, the data of the address pointed to by p is constant.
The sample code here is the same as above
int *const p
const is placed to the right of *, so it modifies p, that is, p cannot be changed, that is, the address pointed to by p is unchanged, but the value stored in this address can be changed
The following is an example:
Error example: (want to change the address pointed to by p)
int main() { int n = 0; int* const p = &n; int m = 100; p = &m; }
Correct example:
What is changed is the content of p that address, not the location of this address
int main() { int n = 0; int* const p = &n; *p = 100;//Can change * p printf("%d", *p); }
const int *const p
There are two consts here. For the first const, it is on the left of * and it modifies * p, that is, * p is unchangeable. For the second const, it is on the right of the star, so it modifies p, that is, the address pointed to by p is unchanged. Combined, it means that both the address and the value stored in the address are unchangeable
There is no good example here, because * p and p cannot be changed.
summary
For const modified pointers, the important thing is to look at its position with *
Expanding thinking on secondary pointer and const
const int** ppa
int main() { int a = 10; int* pa = &a; int m = 100; int* pb = &m; //const modifies * * pa const int** ppa = &pa; //Wrong writing method: you cannot change * * ppa, but * ppa can be changed, and ppa can also be changed //**ppa = 100; //Correct writing: *ppa = &m; printf("%d\n", **ppa); m = 1000; ppa = &pb; printf("%d", **ppa); return 0; }
int* const* ppa
int main() { int a = 10; int* pa = &a; int m = 100; int* pb = &m; int* const* ppa = &pa; //Error example: const modifies * ppa, so * ppa cannot be changed, but ppa can and * * ppa can //*ppa = &m; //Correct example: ppa = &pb; printf("%d\n", **ppa); **ppa = 1000; printf("%d", **ppa); }
int** const ppa
int main() { int a = 10; int* pa = &a; int m = 100; int* pb = &m; int** const ppa = &pa; //const modifies ppa, so ppa cannot be changed //Error example //ppa = &pb; //Correct example: * ppa and * * ppa can be changed *ppa = pb; printf("%d\n", **ppa); **ppa = 1000; printf("%d\n", **ppa); return 0; }
Why does const appear?
Every cause has its fruit. C language has experienced a long development before it has some current grammars. The emergence of const must have its reason.
const and misoperation
Let's take a look at the function strcpy that implements string copying at the bottom of the C language. Why should we set const here? We know that we want to copy the string pointed to by the character pointer strSource to strDestination. If we accidentally misoperate strSource inside the implementation of this function, will the result of copying the past be correct However, the use of const is actually a protective measure. It is more convenient for the compiler to know that this variable needs constant quantization and cannot be changed. It warns us in time
PS: after the exam, I just made up my blog. If you see any problems, please correct me!!!