Understand the complex statement of C in C

xiaoxiao2021-03-05  21

Problem: Given 7 4 * 4 arrays, you need to define an array containing 7 elements, each element of the array is the first address of these 7 arches, and cannot be used as int P [7] [4] [4 ] = {7 array definitions ....} ;. But Use. Solving the method has the easiest type, but not recommended, it is to direct the address schedule to the VOID * type, the mandatory task is handed over to the compiler. One of the ways to benefit the opportunity to make a supplement The grammar rules declared in the C language of BT, in the <

> There is such a paragraph: Understand the priority rules of C language statement: 1) Statement starts from its name, then read in sequence in priority 2) Priority from high to low order is a) declaration Part B) Suffix Operator: Bracket () Represents This is a function, square bracket indicates this an array C) prefix operator, * indicates this pointer 3) If there is const or volatile back tight Type spectrum such as int, then it acts on type specifier, in other cases, constly, etc., the adjacent pointer is as an asterisk back to the problem, suppose the sheet1 ~ sheet7 you want to use is int [4] [4] Array, then you can do this: typedef int (* ptr) [4] [4]; (Be careful not to write TypedEf (int * PTR) [4] [4];) define a new type, According to the above three guidelines: first in parentheses, PTR is an INT type pointer, then there is a harder [], so the PTR is a pointer to the 4 * 4 array. This will be used later: PTR p [7] = {& Sheet1, & Sheet2, & Sheet3, & Sheet4, & Sheet5, & Sheet6, & Sheet7}; If you have understood the above rules, you can write it directly without TypeDef, directly ((* p [7]) [4] [4]) = {/ * Be careful not to write (int * p [7]) [4] [4]; * / & sheet1, & sheet2, & sheet3, & sheet4, & sheet5, & sheet6, & sheet7}; reading is: p It is an array containing 7 elements because there is a * number, so that the part in parentheses can be understood as a group containing 7 pointers; what is the pointing point of the pointer? Answer in the previous group: The pointer points to the INT type 4 * 4 array (huh, I almost painted ...) The above is how to understand the guidelines and examples of the statement of BT in C, I hope that it is clear. BTW1: above If the array is initialized if there is no & number before 4 * 4, then you can compile the past but will have a warning (I use GCC), but I can't go (G ) in C , prompt is "error: Cannot Convert CONVERT) INT (*) [4] 'to `int (*) [4]' in Initialization", that is to say it regards Sheet1's number of group names as int (*) [4], and The number of group names as a secondary pointer will be seen in the future, thereby seeing C and C. Some of the time when the type is handled. BTW2: When accessing the elements in the 4 * 4 array passing the array P, note that P is a pointer thus to decompose before taking the element, such as (* p [0 ]) [0] [0], if the previous declusion is used as P [0] [0] [0], then the P is used as a 3-dimensional number of groups, and the consequences can imagine ..... BTW3: If there is no identification of the type of pointer, there is a stupid method, using GDB, such as for Sheet1, I entered "P & Sheet1" in GDB, which is the address of the number of variable sheet1, at this time, GDB will print at the same time The type of this variable, here is: int (*) [4] [4], notice the middle * Description This type is a pointer, so join us next to the * number can be, such as Int here is int (* p [7]) [4] [4], so we can also write Typedef: typedef int (* ptr [7]) [4] [4]; (Note Do not write into typedef (int * ptr [7] [4] [4];) PTR P = {& Sheet1, & Sheet2, & Sheet3, & Sheet4, & Sheet5, & Sheet6, & Sheet7};

转载请注明原文地址:https://www.9cbs.com/read-38318.html

New Post(0)