1.函数指针与回调函数 指向函数的指针
参考链接:1.https://www.cnblogs.com/mrsandstorm/p/6150092.html
2.https://blog.csdn.net/qq_43710979/article/details/88030147?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
【注意】函数指针它是个指针,指向的是某个函数的地址,比如它指向fun函数
写法如下:int (*pfun) (int,int)或者int (*pfun) (int x,int y),这就代表pfun这个函数指针,指向fun函数,也就是存着fun函数的地址,其中fun函数名就是函数的入口也就是函数的地址。
其中:它的写法比如其返回值,参数都是和它要指向的函数里面的一样,其中名字是(*name)。基本声明形式:返回数据类型 + (*函数名) + (变量类型1,…);
注意 * 和函数名要用括号括起来,否则因为运算符的优先级原因就变成指针函数了
int (*fun) (int);
#include<stdio.h>
int add(int x,int y)
{
return x + y;
}
int (*fun) (int,int); //声明函数指针
int main()
{
fun = &add; //fun函数指针指向add函数
printf("%d ",fun(3,5));
printf("%d",(*fun)(4,2));
return 0;
}
回调函数其实就是一个通过函数指针调用的函数!假如你把A函数的指针当作参数传给B函数,然后在B函数中通过A函数传进来的这个指针调用A函数,那么这就是回调机制。A函数就是回调函数,而通常情况下,A函数是系统在符合你设定条件的情况下会自动执行
回调函数参考链接:https://www.cnblogs.com/jontian/p/5619641.html
2.指针函数 返回指针的函数
【注意】指针函数他是个函数,返回值是某个类型的指针
写法:int* fun(int x, int y){};
int* fun(int,int);
int * fun(int,int);
int *fun(int,int);
#include<stdio.h>
int* fun(int* x) //传入指针
{
int* tmp = x; //指针tmp指向x
return tmp; //返回tmp指向的地址
}
int main()
{
int b = 2;
int* p = &b; //p指向b的地址
printf("%d",*fun(p));//输出p指向的地址的值
return 0;
}