我的下面的代码运行良好。但我有一点疑虑,请看 //Comment1
和 //Comment2
#include<stdio.h>
#include<string.h>
struct PTR
{
int (*funptr)(int);
};
int fun1(int)
{
printf("Fun1\n");
return 0;
}
int fun2(int)
{
printf("Fun2\n");
return 0;
}
int main()
{
PTR p;
p.funptr = &fun1; //Comment1
p.funptr(5);
printf("\n");
p.funptr = fun2; //Comment2
p.funptr(5);
return 0;
}
输出: Fun1 Fun2
输出没有问题。
在comment1中使用了'&'运算符,所以我们明确告诉它获取地址,在comment2中,我们没有使用'&',那么哪种方式是正确的呢?