Function pointers and pointer functions are easy to confuse

In the process of learning arm, I found that this "pointer function" and "function pointer" are easy to make mistakes. The easiest way to distinguish is to see if the pointer * in front of the function name is included in parentheses (). If it is included, it is a function pointer, and vice versa. is a pointer function.

Today we figured it out from scratch

 

First of all the definitions between them:

 

1. A pointer function refers to a function with a pointer, that is, it is essentially a function , and the return type of the function is a pointer of a certain type .

type identifier * function name (parameter list)

int *f(x,y);

First of all it is a function, but the return value of this function is an address value. The function return value must be accepted by the same type of pointer variable, that is to say, the pointer function must have a function return value, and, in the calling function, the function return value must be assigned to the same type of pointer variable.

Express:

float *fun();
float *p;
p = fun(a);

 

Let's talk about it in detail! please look below

Note that pointer functions are different from function pointer representations, so don't confuse them.

 

Pointer functions:
When a function declares that its return value is a pointer, it actually returns an address to the calling function for use in expressions that require a pointer or address.
Format:
type specifier * function name (parameter)
Of course, since the returned address is an address, the type specifier is generally int.

E.g:

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

 

函数返回的是一个地址值,经常使用在返回数组的某一元素地址上。

复制代码
 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 }
复制代码

程序应该是很好理解的,子函数返回的是数组某元素的地址。输出的是这个地址里的值。

 

 

2、函数指针是指向函数的指针变量本质是一个指针变量。

    int (*f) (int x); /*声明一个函数指针 */

   f=func; /* 将func函数的首地址赋给指针f */

指向函数的指针包含了函数的地址的入口地址,可以通过它来调用函数。声明格式如下:
类型说明符 (*函数名)   (参数)
其实这里不能称为函数名,应该叫做指针的变量名。这个特殊的指针指向一个返回整型值的函数。指针的声明笔削和它指向函数的声明保持一致。
指针名和指针运算符外面的括号改变了默认的运算符优先级。如果没有圆括号,就变成了一个返回整型指针的函数的原型声明。
例如:

void (*fptr)();

把函数的地址赋值给函数指针,可以采用下面两种形式:

fptr=&Function;
fptr=Function;

取地址运算符&不是必需的,因为单单一个函数标识符就标号表示了它的地址,如果是函数调用,还必须包含一个圆括号括起来的参数表。
可以采用如下两种方式来通过指针调用函数:

x=(*fptr)();
x=fptr();

第二种格式看上去和函数调用无异。但是有些程序员倾向于使用第一种格式,因为它明确指出是通过指针而非函数名来调用函数的。

下面举一个例子:

复制代码
 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 }
复制代码

 

程序输出为:

FileFunc
EditFunc

 

主要的区别是一个是指针变量,一个是函数。在使用是必要要搞清楚才能正确使用

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325920548&siteId=291194637