1.A pointer to a pointer variable is called a pointer.
Definition form: Type ** variable name
For example: int I, * p, ** q;
I = 30;
P = & I;
Q = & p; // pointer q points to p pointer
2.Pointers and functions
A. the pointer is used as a function parameter.
B. pointer Functions
C. pointer to function
A. Example:
Swap (int * pa, int * pb)
{
Int p;
P = * pa;
* Pa = * pb;
* Pb = p;
}
Main ()
{
Int a, B;
Int * p1, * p2;
Scanf ("% d", & a, & B );
P1 = & a, p2 = & B;
If (a <B)
Swap (p1, p2 );
Printf ("\ n % d, % d \ n", a, B );
}
Input from keyboard: 5 10
Output: 10, 5
Let's look at the following example.
Main ()
{
Int a, B; int * p1, * p2;
Void swap (int * pa, int * pb );
Scanf ("% d, % d, & a, & B );
P1 = & a; p2 = & B;
If (a <B)
Swap (p1, p2 );
Printf ("\ n % d, % d \ n", a, B );
}
Void swap (pa, pb)
{
Int * pa, * pb;
Int * p;
P = pa;
Pa = pb;
Pb = p;
}
Input from keyboard: 5 10
Output: 10, 5
Pointer function:
A function can return integer, real, and balanced values, or pointer-type data, that is, an address.
The return value is a pointer function.
The general definition form is:
Type identifier * function name (parameter table );
Example: int * a (int x, float y)
{......}
A is the function name. After calling it, you can get a pointer (Address) to the integer data)
X and y are the form parameters of function.
There is a * in front of this function, indicating that the sub-function is a pointer type function.
For example:
Main ()
{
Int a, B, * p;
Int * max (int, int );
Scanf ("% d", & a, & B );
P = max (a, B );
Printf ("max = % d", * p );
}
Int * max (int x, int y)
{If (x <y)
Return (& y );
Else return (& x );
Pointer to function:
The function name indicates the function entry address.
Pointer to a function: type (* pointer variable name )();
Example: int (* p )();
Example:
Int max (...)
{......}
Int (* p) (); // defines the pointer variable pointing to the Function
P = max; // enables p to point to the entry address of the max function. You can call max through (* p) ().
Example:
Main ()
{
Int max ();
Int (* p) (); // defines the function pointer
Int a, B, c;
P = max;
Scanf ("% d \ n", & a, & B );
C = (* p) (a, B); // call a function
Printf ("a = % d, B = % d, max = % d", a, B, c );
}
Int max (x, y)
{
Int z;
If (x> y) z = x;
Else z = y;
Return (z );
}