1. array Definition
An array consists of the type, name, [], and length of an array element.
IntA [10] = {0};//Explicit array length: 10IntB [] = {1,2,3};//Implicit array length 3
2. pointer Definition
C ++ uses the * symbol to declare an identifier as a pointer.
Int* P1, * P2;//P1 and P2 point to an intString* Pstring;//Pstring points to a string
3. function pointer Definition
Function pointers consist of identifiers, return values, and parameter lists
Void(* Funcptr )(Int,Int);
When reading such complex statements, the general method is to start from the middle and expand to both sides in sequence.
Starting from the center, it refers to the variable name, which is also funcptr in this example. Search for the nearest element (none) on the right of the variable, then go to the left (*, indicating that funcptr is a pointer), and then right (INT, INT), parameter list ), then the left side (void, return value ). Therefore, funcptr is a pointer to a function of the void type that points to the parameter list as (INT, INT.
Here are some complex examples:
Void* (* FP1 )(Int))[10];
FP1 is near the right side, and * on the left side. Therefore, FP1 is a pointer. The parameter list (INT) on the right side of FP1 indicates that FP1 is a function pointer and the function parameter list is (INT ), therefore, the rest should be the return value of the function. If fp_ret is used to simplify the process, it can be expressed:
Void* (* Fp_ret )[10];
Fp_ret is a pointer pointing to an array with a length of 10. The element type in the array is void *. Therefore, in combination, FP1 is a function pointer that points to a function that has an int type parameter and returns a pointer, this pointer points to an array with 10 void * elements.
TypedefDouble(* Fp3 )())[10]) (); Fp3;
Fp3 is a function pointer. It points to an empty function parameter. The returned value is a pointer pointing to an array with a length of 10, an array element is a function pointer with a NULL return value of the double type in the parameter list.
How should we understand complicated pointer structures?