++++++++++++++++++++++++++++++++++++++++++
"C Language Deep Understanding series-iron Cloth Shirt: Const"
See other blog posts please focus on the original author.
This article is the original site, welcome to reprint! Reprint please indicate the source:
http://blog.csdn.net/mr_raptor/article/details/7210704
++++++++++++++++++++++++++++++++++++++++++
Iron Cloth Shirt: Const
Description: Legend of the C World appeared a need equipment const, it can appear, so that all the assassins, hackers are unemployed, under its protection, all the variables can be intact.
role:The const is constant, said the sea withered Shilan, constant unchanged, 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: readonly.
While understanding the const is relatively easy to understand, the const is not only used for rhetorical basic types, it is also often used to figure out some structural types and pointers and their parameters: arrays, pointers, array of pointers, arrays of structures, arrays of structures, etc. Once connected with these complex types, there is a certain degree 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 take a sip of air conditioning: To say love you, is not an easy thing. However, I have two strokes to identify the use of the skills: to remove the type of the const rhetoric who, who has the iron cloth shirt, whose value can not be modified, ReadOnly.
1. Remove type int becomes: const A = 10,a has the iron cloth shirt, the value of a is unchanged.
2. Remove type int becomes: const A = 10,a has the iron cloth shirt, the value of a is unchanged, the two effects are the same.
3. Remove type int to: const A[10],A[10] has the iron cloth shirt, the value of a array is unchanged.
4. Const rhetoric *P, remove type int becomes: The const *P,*P owns the Iron Sweater (space 2 in the figure below), and the value in the space that P points to is unchanged.
5. Const rhetoric p, remove type int* into: const p, pointer variable p has an iron sweater (space 1 in the figure below), the value in the pointer variable p is unchanged, which means that P can no longer point to other addresses, but the value in the space that P points to is variable, as shown in Figure xxx.
Figure XXX pointer variable and pointer variable pointing to the space diagram
6. Remove the type struct devices into: const DEV[5],DEV[5] has the iron cloth shirt, dev[5 the value in the array is unchanged.
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 cloth shirt, and the values in the space pointed to by each element in the dev of the pointer array are unchanged.
++++++++++++++++++++++++++++++++++++++++++
"C Language Deep Understanding series-iron Cloth Shirt: Const"
See other blog posts please focus on the original author.
This article is the original site, welcome to reprint! Reprint please indicate the source:
http://blog.csdn.net/mr_raptor/article/details/7210704
++++++++++++++++++++++++++++++++++++++++++