Differences between pointer functions and function pointers [reprinted]
I,
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.