When I saw page 12 of the C programming language page112 and talked about complex pointers, I was always in awe of this. Although I read "C and pointer" again in my sophomore year, however, I forgot to use it for a long time. I am going to study it well today.
The complex pointer parsing in the article referenced by supermegaby does not mean plagiarism. I just want to keep it for reference. If I accidentally observe this article, I would like to look up on the original article.
The first is an interview question "What does int (* A [10] (INT) mean ?"
If the pointer is not properly understood, I believe that this question is still beyond the scope of the problem. According to the method in the supermegaby article, it is:
[10] on the right of a indicates that A is an array, and then there is a * on the left, indicating that the element of the array is a pointer, and (INT) on the right of the brackets ), the pointer type is a function pointer. The Pointer Points to a function that has an int type parameter. The leftmost int indicates that the return value of the function is int. therefore, the answer to this question is:The element of array A is a pointer of the function type. It points to a function that has an int type parameter, and the return value type is int.
First, I should clearly describe what I think is a little interesting, so as not to forget: the position of * in front of the label is very important, and the order in which the meaning is different.
* It means a pointer. The specific Pointer Points to what needs to be analyzed both before and after.
For example, int A [10] means that A is an array, and the array element is an integer.
INT (* A) [10] means: A is a pointer to an array, and the elements of the array are integers.
Int * A [10] indicates that A is an array, and the elements of the array are integer pointers.
It seems that what we mentioned above is the pointer array and the array pointer. I cannot remember which one is specific. Check Shi Hu's C and add it here.
Int * a () indicates that the return value of function a is of the int * type.
INT (* A) () indicates that a is a function pointer, and the function to which it points has a null parameter. The returned value is int.
This type of question can be understood as a three-stage question:[(Array type) (identifier) (array)] is combined in [(function return value) (identifier) (function)] and in [(pointer) (identifier) (null )]Which of the following types should be distinguished according to the third section ([] means an array, and the first section is an array type; relative to it, () means a function, the first section returns the function value)
Onion method analysis:
The Onion method is like the onion peeled by Yang zongwei, but it is determined from the inside out: the order of each layer is (2-3-1). First, the second part is located, then, based on the third section, peel the onion and determine whether it is an array or a function. Then, the result is obtained from the first section.Array Element typeOrType of the function return value. Removing each bracket layer by layer is like peeling off each layer of onion until the solution is completed.
Note: The feature of this type of question is that the type of the first segment is always a pointer, because only in this way can it be associated with the next layer of onion.
Specific instance:
INT (* func [5]) (int * P );
The right side of func is a [] Operator, indicating that func is an array with five elements, and there is a * on the left of func, indicating that the element of func is a pointer, note that * not modifying func, but modifying func [5] is because the [] operator has a higher priority than *, and func is first combined, therefore, * modifies func [5]. Jump out of this bracket and look at the right. It is also a pair of parentheses, indicating that the element of the func array is a pointer of the function type. It points to the function with an int * type parameter and the return value type is int.
(Onion method: func is an array. The element of the array is a pointer. What does the pointer point? The Pointer Points to a function. The parameter of the function is int * P, and the return value of the function is int.That is, the element of the func array is a pointer of the function type. The function it points to has an int * type parameter, and the return value type is int.)
INT (* func) [5]) (int * P );
Func is enclosed by parentheses, and there is a * on the left. func is a pointer, out of parentheses, and a [] operator number on the right, which indicates that func is a pointer to an array, to the left, there is a "*" on the left, indicating that the element of this array is a pointer, and then the brackets appear on the right, indicating that the element of this array is a pointer to the function. To sum up, func is a pointer to an array. The elements of this array are function pointers. These pointers point to functions with int x parameters and return values of the int type.
(Onion method: func is a pointer. What does the pointer point? After a layer is removed, the pointer points to an array. The element of the array is a pointer. What does it point? Remove one layer and point to a function. The parameter of the function is int * P, and the return value of the function is int.That is, func is a pointer to an array. The elements of this array are function pointers. These pointers point to functions with int x parameters and return values of the int type.)
According to the three-part principle, this field * must be added, indicating that the array element is a pointer; otherwise, INT (* func) [5]) (int * P); the type of the array element is unknown.
INT (* func) (int * p) [5];
Func is a function pointer. This type of function has an int * type parameter. The returned value is a pointer to an array. The elements of the array to which it points are arrays with five int elements.
(Onion method: func is a pointer. What does the pointer point? The Pointer Points to a function. The parameter of the function is int *, and the returned value is Pointer. What does the pointer point? After a layer is stripped, the pointer points to an array. The length of the array is 5, and the type of each primary color of the array is int,That is, func is a function pointer. This type of function has an int * type parameter, and the return value is a pointer to an array. The elements of the array to which it points are arrays with five int elements.)
Char (* X () []) ()
X is a function without parameters. Its return value is a pointer to an array. The element of this array is a function pointer. The function to which X points has no parameters and returns char.
(Onion method: X is a function, which is an invisible parameter. The returned value is a pointer. What does this pointer point? The Pointer Points to an array. The element of the array is a pointer. What does this pointer point? At the layer of stripping, the pointer points to a function with an invisible parameter returned as char.That is, the no-argument function x returns an array pointer. The array it points to is a pointer array. Each pointer element of the array points to no parameter, and the return value is Char.)
Char (* X [3]) () [5]
X is a function pointer array. If the function has no parameters, the array pointer of char (*) [5] is returned.
(Onion method: X is an array. The element of the array is a pointer. What does the pointer point? The Pointer Points to a function. If this function does not return a pointer, what is the pointer returned by the function? The Pointer Points to an array with a length of 5. The elements of the array are of the char type.That is, the element of array X is a pointer of the function type. The function to which it points has no parameters. The returned value is an array pointer of char (*) [5.)