I. pointer functions and function pointers
When I learned about arm, I found that the "pointer function" and "function pointer" are easy to make mistakes. So today, I want to figure it out and find some information, first, the definitions between them:
1. A pointer function is a function with a pointer. A function returns a pointer of a certain type.
Type identifier * function name (parameter table)
Int * f (x, y );
First, it is a function, but the return value of this function is an address value. Function return values must be accepted by pointer variables of the same type. That is to say, pointer functions must have function return values. In addition, in the main function, function return values must be assigned to pointer variables of the same type.
Indicates:
Float * Fun ();
Float * P;
P = fun ();
Note that pointer functions and function pointers indicate different methods. Do not confuse them. The simplest way to identify a function is to check whether the pointer * in front of the function name is included in parentheses (). If it is included, it is a function pointer. Otherwise, it is a pointer function.
Let's talk about it in detail! See the following
Pointer function:
When a function declares that its return value is a pointer, it actually returns an address to the called function to be used in an expression that requires a pointer or address.
Format:
Type specifier * function name (parameter)
Of course, because an address is returned, the type specifiers are generally Int.
Example: int * getdate ();
Int * AAA (INT, INT );
The function returns an address value, which is often used on an element address of the returned array.
Int * getdate (INT wk, int Dy );
Main ()
{
Int wk, Dy;
Do
{
Printf (enter Week (1-5) Day (1-7) \ n );
Scanf (% d, & wk, & Dy );
}
While (WK <1 | Wk> 5 | dy <1 | dy> 7 );
Printf (% d \ n, * getdate (wk, Dy ));
}
Int * getdate (INT wk, int Dy)
{
Static int calendar [5] [7] =
{
{1, 2, 3, 4, 5, 6, 7 },
{8, 9, 10, 11, 12, 13, 14 },
{15,16, 17,18, 19,20, 21 },
{22, 23, 24, 25, 26, 27, 28 },
{29,30, 31,-1}
};
Return & calendar [wk-1] [DY-1];
}
The program should be well understood. The subfunction returns the address of an element in the array. The output is the value in this address.
2. A function pointer is a pointer variable pointing to a function, which is essentially a pointer variable.
INT (* f) (int x);/* declare a function pointer */
F = func;/* assign the first address of the func function to the pointer f */
The pointer to a function contains the address of the function, which can be used to call the function. The Declaration format is as follows:
Type specifier (* function name) (parameter)
In fact, it cannot be called the function name. It should be called the variable name of the pointer. This special Pointer Points to a function that returns an integer value. The pointer statement is the same as the statement pointing to the function.
Parentheses outside the pointer and pointer operators change the default operator priority. Without parentheses, it becomes a prototype declaration of a function that returns an integer pointer.
For example:
Void (* fptr )();
Assign the function address to the function pointer in the following two forms:
Fptr = & function;
Fptr = function;
It is not necessary to use the address operator, because a Function Identifier represents its address. If it is a function call, it must also contain a parameter table enclosed in parentheses.
You can use the following two methods to call a function through a pointer:
X = (* fptr )();
X = fptr ();
The second format looks like a function call. However, some programmers tend to use the first format because it explicitly states that the function is called through pointers rather than function names. The following is an example:
Void (* funcp )();
Void filefunc (), editfunc ();
Main ()
{
Funcp = filefunc;
(* Funcp )();
Funcp = editfunc;
(* Funcp )();
}
Void filefunc ()
{
Printf (filefunc \ n );
}
Void editfunc ()
{
Printf (editfunc \ n );
}
Program output:
Filefunc
Editfunc
The main difference is that one is a pointer variable and the other is a function. It is necessary to make it clear before it can be used correctly.
2. pointer
The pointer seems confusing. Their declaration has two asterisks. For example:
Char ** CP;
If there are three asterisks, It is the pointer of the pointer, the four asterisks are the pointer of the pointer, and so on. When you are familiar with simple examples, you can cope with complex situations. Of course, in the actual program, only the second-level pointer is used. The three asterisks are not common, let alone the four asterisks.
The pointer must use the pointer address.
Char c = 'a ';
Char * P = & C;
Char ** CP = & P;
The pointer can not only access the pointer it points to, but also access the data pointed to by the pointer it points. The following are several examples:
Char * P1 = * CP;
Char C1 = ** CP;
You may want to know how this structure works. The pointer allows the called function to modify local pointer variables and process pointer arrays.
Void findcredit (INT **);
Main ()
{
Int Vals [] = {, 5,-, 0 };
Int * fp = Vals;
Findcredit (& FP );
Printf (% d \ n, * FP );
}
Void findcredit (INT ** FPP)
{
While (** FPP! = 0)
If (** FPP <0) break;
Else (* FPP) ++;
}
First, use an array address to initialize the pointer FP, and then pass the pointer address as a real parameter to the findcredit () function (). The findcredit () function indirectly obtains the data in the array through the expression ** FPP. To traverse the array to find a negative value, the findcredit () function performs auto-increment operations on the object that the caller points to the array instead of its own pointer to the caller pointer. The statement (* FPP) ++ is used to perform auto-increment operations on the pointer pointed to by the form parameter pointer. However, because the * operator is higher than the ++ operator, parentheses are required here. If no parentheses exist, the ++ operator applies to the double pointer FPP.
3. pointer to the pointer Array
Another method of pointer is to process the pointer array. Some programmers prefer to use pointer arrays instead of multi-dimensional arrays. A common usage is to process strings.
Char * Names [] =
{
Bill,
Sam,
Jim,
Paul,
Charles,
0
};
Main ()
{
Char ** Nm = names;
While (* NM! = 0) printf (% s \ n, * NM ++ );
}
First, initialize the pointer nm with the address of the struct pointer array names. Each call to printf () first passes the attention pointer pointed to by the pointer nm, and then carries out an auto-increment operation on the NM so that it points to the next element (or pointer) of the array ). Note that the preceding syntax is * NM ++. It first obtains the content pointed to by the pointer and then auto-increment the pointer.
Note that the last element in the array is initialized to 0, and the while loop is used to determine whether it is at the end of the array. Pointers with zero values are often used as terminator for loop arrays. The programmer calls the zero-value pointer a null pointer ). The NULL pointer is used as the Terminator. When adding or deleting elements to the tree type, you do not need to modify the code for traversing the array, because the array still ends with a null pointer.