定义方式:int (*p)(int x, int y);
#include <stdio.h>
int sum(int x, int y){
return x + y;
}
int reduce(int x, int y){
return x - y;
}
int multiply(int x, int y){
return x * y;
}
float divide(int x, int y){
if(y ==0)
return 0;
else
return x *1.0f/ y;
}
struct Student{
int age;
float weight;
char *name;
void(*studentP());
}
int main(int argc, const char * argv[])
{
// 1.定义函数指针
int (*p)(int x, int y);
// 2.初始化
p = sum;
// 3.赋值
int s = p(3,2);
// 4.打印
printf("%d\n",s);//=5;
//--------------
printf("请输入数字1,2,3,4\n");
int num;
scanf("%d",&num);
switch (num) {
case 1:
p = sum;
break;
case 2:
p = reduce;
break;
case 3:
p = multiply;
break;
case 4:
p = divide;
break;
}
int result = p(22,2);
printf("%d\n",result);
//结构体:
struct Student stu = (22,88,"tom",studp);
printf("结构体长度=%d",sizeof(stu));
//1.
stu.studp;
//2.
struct stu* stp = &stu;
(*stp).studp;
//3.
stp -> studp;
return 0;
}