I'm learning arm In the process, it was found that “ Pointer function ” And “ A function pointer ” It's easy to get wrong , The easiest way to tell is to look at the pointer in front of the function name * The number is not in parentheses （） contain , If it is included, it is a function pointer , Otherwise, it's a pointer function .
Today, let's get it straight from the beginning
First of all, the definition between them ：
1、 A pointer function is a function with a pointer , That is, the essence is a function , A function return type is a pointer to a type .
Type identifier * Function name ( Parameter table )
First it's a function , It's just that the return value of this function is an address value . The return value of a function must be a pointer variable of the same type , in other words , A pointer function must have a function return value , and , In the main function , The return value of a function must be assigned to a pointer variable of the same type .
p = fun(a);
Let's be more specific ！ Please see the following
Note the difference between pointer function and function pointer representation , Don't confuse .
Pointer function ：
When a function declares that its return value is a pointer , In fact, it returns an address to the calling function , To be used in expressions that require pointers or addresses .
Type specifier * Function name ( Parameters )
Yes, of course , Because it returns an address , So type specifiers are generally int.
for example ：
int * aaa(int,int);
Function returns an address value , It is often used to return the address of an element of an array .
1 int * GetDate(int wk,int dy);
4 int wk,dy;
6 printf(Enter week(1-5)day(1-7)\n);
13 int * GetDate(int wk,int dy)
15 static int calendar=
23 return &calendar[wk-1][dy-1];
The program should be easy to understand , The sub function 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 to a function , That is, it is essentially a pointer variable .
int (*f) (int x); /* Declare a function pointer */
f=func; /* take func The first address of the function is assigned to the pointer f */
The pointer to the function contains the entry address of the function's address , You can use it to call functions . The format of the statement is as follows ：
Type specifier (* Function name ) ( Parameters )
In fact, it can't be called function name , The variable name that should be called a pointer . This particular pointer points to a function that returns an integer value . The declaration stroke of a pointer is consistent with the declaration it points to a function .
The brackets outside the pointer name and pointer operator change the default operator priority . If there are no parentheses , It becomes a prototype declaration of a function that returns an integer pointer .
for example ：
Assign the address of the function to the function pointer , It can take the following two forms ：
Fetch address operator & Not required , Because a single function identifier denotes its address , If it's a function call , It must also contain a parameter list enclosed in parentheses .
You can call functions through pointers in two ways ：
The second format looks like a function call . But some programmers tend to use the first format , Because it explicitly states that a function is called through a pointer rather than a function name .
Here's an example ：
1 void (*funcp)();
2 void FileFunc(),EditFunc();
12 void FileFunc()
17 void EditFunc()
The program output is ：
The main difference is that one is a pointer variable , One is a function . It is necessary to make it clear before using it correctly