Array pointer, pointer array, function pointer, pointer function, array pointer Function
1. Four access methods for Arrays
Define array a []; pointer * p points to array;
(1) access a [I] using the table below the array;
(2) array name + address offset I * (a + I)
(3) Use Pointer p to access table p [I]
(4) offset of pointer p + address I * (p + I)
One-dimensional array name: equivalent to a single pointer
2.
Array pointer Array)
Function pointer function --------> just look at the last two words to distinguish between pointers, arrays, or functions.
_____________________________________________________________________________
Array pointer: (pointer to all array addresses)
Int a [3] = {2, 3, 4}
Int * p = a; // pointer to the array
Int * p1 = & a [0] // pointer to the first element of the array
Int (* p2) [3] = & a; // The array Pointer Points to all the addresses of the array
3-> Number of data points to the array
& A indicates all the address spaces from the beginning to the end of the array.
_____________________________________________________________________________
Pointer array: (an array that stores multiple addresses)
Int a = 10;
Int B = 20;
Int c = 30;
Int * pa [3] = {& a, & B, & c}; // The int * address is saved)
_____________________________________________________________________________
Function number pointer (pointer to function) // function name indicates the function entry address
Three elements: the return value type is the same as that of the function, the parameter type is the same, and the number of parameters is the same.
Int Max (int a, int B ){}
Int (* pfun) (int, int );
Main ()
{
Max (12, 13 );
Pfun = & Max; ----> pfun = Max;
(* Pfun) (12,13); ----> pfun (12,13 );
}
_____________________________________________________________________________
Pointer function (return type is pointer type called pointer function)
Definition Format: return type identifier * function name (form parameter table)
{Function body}
_____________________________________________________________________________
3. How to differentiate:
Array pointer:
Define int (* p) [n]; () with a high priority. p is a pointer pointing to an integer one-dimensional array,
The length of this one-dimensional array is n.
Pointer array:
Defines int * p [n]; []. The priority is high. First, it is combined with p to form an array. Then, int * indicates that this is an integer.
Pointer array, which has n Array elements of pointer type.
Function pointer:
Define int (* pfun) (int, int); * pfun is enclosed in brackets. First, a pointer is formed,
Point to a function whose return value is an integer, and this function has two integers as the form parameters.
Pointer function:
Define int * fun (); equivalent to int * fun (); return value is an integer pointer.
_____________________________________________________________________________
4. Complex pointers:
Right left rule: read from undefined identifiers, then read from right to left. Every time you encounter parentheses, the orientation is dropped.
Once everything in the parentheses is parsed, the parentheses appear. Repeat this process until the resolution is complete.
Int (* fun) (int * p );
First, find the undefined identifier, that is, fun, which has a pair of parentheses and a * sign on the left.
Fun is a pointer, and then jumps out of this parentheses. first look at the right and it is also a parentheses. This shows that (* func) is a function,
Fun is a pointer to this type of function. It is a function pointer. This type of function has an int * type parameter and the return value type is int.
Int (* fun [5]) (int * p );
Fun is a [] on the right, indicating that fun is an array with 5 elements, and there is a * on the left of fun, indicating that the element of fun is a pointer,
Note that * not modifying fun, but modifying fun [5] is because the [] operator has a higher priority than *, and fun is first combined,
Therefore, * modifies func [5]. Jump out of this bracket and look at the right side. It is also a pair of parentheses, indicating that the elements of the func array are of the function type.
Pointer, which indicates that the function has an int * type parameter, and the return value type is int.