The difference between pointer function and function pointer

King of heaven and earth tiger 626 2021-04-03 11:50:25 阅读数:726

本文一共[544]字,预计阅读时长:1分钟~
difference pointer function function pointer

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 )

int *f(x,y);
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 .

Express :

float *fun();
float *p;
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 .
Format :
Type specifier * Function name ( Parameters )
Yes, of course , Because it returns an address , So type specifiers are generally int.
for example :

int *GetDate();
int * aaa(int,int);
 

Function returns an address value , It is often used to return the address of an element of an array .
Copy code
 1 int * GetDate(int wk,int dy);
 2 main()
 3 {
 4 int wk,dy;
 5 do{
 6 printf(Enter week(1-5)day(1-7)\n);
 7 scanf(%d%d,&wk,&dy);
 8 }
 9 while(wk<1||wk>5||dy<1||dy>7);
10 printf(%d\n,*GetDate(wk,dy));
11 }
12 
13 int * GetDate(int wk,int dy)
14 {
15 static int calendar[5][7]=
16 {
17 {1,2,3,4,5,6,7},
18 {8,9,10,11,12,13,14},
19 {15,16,17,18,19,20,21},
20 {22,23,24,25,26,27,28},
21 {29,30,31,-1}
22 };
23 return &calendar[wk-1][dy-1];
24 }
Copy code
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 :

void (*fptr)();
Assign the address of the function to the function pointer , It can take the following two forms :

fptr=&Function;
fptr=Function;
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 :

x=(*fptr)();
x=fptr();
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 :

Copy code
 1 void (*funcp)();
 2 void FileFunc(),EditFunc();
 3 
 4 main()
 5 {
 6 funcp=FileFunc;
 7 (*funcp)();
 8 funcp=EditFunc;
 9 (*funcp)();
10 }
11 
12 void FileFunc()
13 {
14 printf(FileFunc\n);
15 }
16 
17 void EditFunc()
18 {
19 printf(EditFunc\n);
20 }
Copy code
 

The program output is :

FileFunc
EditFunc
 

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

版权声明:本文为[King of heaven and earth tiger 626]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2021/04/20210403085030806z.html