当前位置:网站首页 >> 产品/行业资讯 >>

函数指针,指针函数,函数指针数组

这是由一群朋友发出的笔试题,涉及的内容也更加有趣。

直接查看代码void& nbsp;(* f [])(char& nbsp; *)这是什么?让我们来看看以下内容。

函数指针和指针函数定义。

让我们看一下代码int& nbsp; * func(int& nbsp; a,int& nbsp; b)之前,我们讨论了运算符的优先级“ *& nbsp;”。

优先级低于“(”),因为“ *”的优先级低于“(())”的优先级;,func首先与以下“()”组合,这意味着func是一个函数。

即:int& nbsp; *(func)(int& nbsp; a,int& nbsp; b)然后此函数的返回值的类型是“& int *& nbsp;”,即:指向int类型的指针,然后我们以这种方式修改上述代码int& nbsp;(* func) (int& nbsp; a,int& nbsp; b)“(* func)”表示func是一个指针,然后跟着“()”表示此指针指向一个函数,也就是说,因此,函数指针:& nbsp;首先是一个指针,此指针指向函数指针函数:首先是一个函数,该函数的返回值是一个指针。

使用typedef声明一个函数指针我们声明一个函数指针,通常的方法是int&amp ; nbsp;(* pfunc)(int& nbsp; a,int& nbsp; b)当我们命名许多函数指针时,上述方法非常不方便,因此我们可以执行typedef& nbsp; int& nbsp;(* PF) & nbsp;(int& nbsp; a,intb)PF& nbsp; pfunc;例行程序:#include& nbsp;“ stdio.h” typedef& nbsp; int(* PF)(int,& nbsp; int); int& nbsp; add(int& nbsp; a,& nbsp; int& nbsp; b){& nbsp; return& nbsp; a& nbsp; +& nbsp; b;} int& nbsp; reduce(int& nbsp; a,& nbsp; int& nbsp; b){& nbsp; return& nbsp; a& nbsp;-& nbsp; b  int& nbsp; main(){& nbsp; PF& nbsp; pfunc& nbsp; =& nbsp; NULL;& nbsp; pfunc& amp; ; nbsp; =& nbsp; add;& nbsp; printf(“ add:%d ",pfunc(3,& nbsp; 4));& nbsp; pfunc& nbsp; =& nbsp; reduce;& nbsp; printf(“ reduce:%d ",& nbsp; pfunc(3,& nbsp; 4));& nbsp; / * getchar用VS编写,以便于查看输出* /& nbsp; getchar();& nbsp; return& nbsp; 0;} img让我们回到void& nbsp;上方的主题(* f [])(char& nbsp; *)f到底是什么? []的优先级高于*的优先级,因此f首先修改数组,然后遵循*组合,这意味着该数组位于内部。

所有存在的对象都是指针。

这些指针是什么?出来后,您会看到。

该指针是一个函数,该函数的参数为​​char *返回值为空。

样本代码#include& lt; stdio.h& gt; void& nbsp;(* f [3])(char& nbsp; *); void& nbsp; efunction(char& nbsp; *& nbsp ; s){& nbsp; printf(“%s " s);} int& nbsp; main(){& nbsp; f [0]& nbsp; =& nbsp; efunction;& nbsp; /// void (** f []) (char& nbsp; *)& nbsp; =& nbsp; {efunction};& nbsp;& nbsp;(* f [0])(“ hello& nbsp; code”);& nbsp; & nbsp;& nbsp;返回& nbsp; 0;}代码输出hello& nbsp; code ------------ ------------ --------使用0.0返回值0.08441和秒后退出处理#请按任意键继续。

&"          " ..& nbsp;。

函数指针在项目中的实际应用。

这是我的android项目的hal部分的代码。

该部分代码使用函数指针,不同的函数按名称调用。

代码的android hal部分可以理解这些内容,我们可以理解,当编写别人的代码时,我们有时会看别人的代码,并且经常感到困惑,例如以下void(* p)();和这个(*(void(*)())0)();我记得在上一篇文章中,我谈到了左右原则。

从p开始,向右转到遇到,然后再向左转到遇到(,(* p),我们可以看到p是一个指针,继续分析Go并遇到(),表明p指向转到左侧,知道p指向的函数的返回值是空的。

好,看下面的示例。

#include& nbsp;“ stdio.h” void& nbsp; Function( ){& nbsp;& nbsp;& nbsp; printf(“ Call& nbsp;& nbsp;& nbsp;功能! "};} int& nbsp; main(){& nbsp;& nbsp;& nbsp;& nbsp; void(* p)();& nbsp;& nbsp;& nbsp;& amp; ; nbsp; *(int *)& p& nbsp; =& nbsp;(int)Function;& nbsp;& nbsp;& nbsp;& nbsp;(* p)();& ;& nbsp;& nbsp; getchar();& nbsp;& nbsp;& nbsp; return& nbsp; 0;}输出结果并继续分析( *(void(*)())0)(); 1. void(*)()& nbsp;我们在上面进行了分析

欢迎您的咨询