Const
Description: legend has it that there is a need in the C world of the best equipment const, it can appear, so that all the world's assassins, hackers are unemployed, under its protection, all the variables can be intact.
role:Const is a shorthand for constant, indicating that the sea withered Shilan, constant, once accompanied, never "heart" change. As long as a variable is preceded by a const rhetoric, it means that the data in the variable can be accessed and cannot be modified. We can actually give it a more elegant name called: readonly.
Although it is relatively easy to understand the const, const is not only used for rhetorical basic types, it is often used to figure out some types of construction and pointers and their parameters: arrays, pointers, pointers, arrays of structures, arrays of pointers to structures, etc. Once combined with these complex types, there is a certain amount of confusion. We analyze each of them.
1 const int a = 10;
2 int const A = 10;
3 const int A[10] = {1,2,3,4,5,6,7,8,9,10};
4 const int *p;
5 int * const p;
6 const struct devices dev[5];
7 struct Devices const * DEV[5];
See the examples listed above, I believe many friends will suck a breath of air: To say that love you, is not an easy thing. However, I have two tricks to identify the use:
- Remove the Type
- Look at the const rhetoric who, who has the iron cloth shirt, whose value is can not be modified, ReadOnly.
1. Remove the type int to: const A = 10,a has an iron sweater, the value of a does not change.
2. Remove the type int into: const A = 10,a has a sweater, the value of a does not change, the two effects are the same.
3. Remove the type int into: const A[10],A[10] has a iron sweater, the value in a array does not change.
4. Const rhetoric *P, removing the type int becomes: The const *p,*p has the iron cloth (medium space 2), the value of the space pointed by P does not change.
5. Const rhetoric p, remove the type int* into: const p, the pointer variable p has a iron cloth (medium space 1), the pointer variable p value is not changed, that is, p can no longer point to other addresses, but p points to a variable value in the space, XXX shows.
Figure XXX pointer variable and pointer variable point to the space
6. Remove the type struct devices to: const DEV[5],DEV[5] has an iron sweater, dev[5] The value in the array does not change.
7. This is an array of pointers to the devices struct type, which has 5 devices struct type pointers, each pointing to a devices struct, const rhetoric *dev[5], removing the type struct devices into: const *DEV[5] , the pointer array *dev[5] has an iron sweater, and the value of each element in the pointer array dev points to the same amount of space.
C language Keywords-iron sweater: const REPRINT