我们在哪里可以使用C函数指针,有什么用函数指针 [英] Where can we use function pointers in C, what uses for function pointers

查看:156
本文介绍了我们在哪里可以使用C函数指针,有什么用函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就在最近,有人问我在接受采访时:我们在哪里可以使用函数指针用C?
什么函数指针的回报。我说,我们使用可以用函数指针调用的函数,然后他问了一些例子,但我不能满足他的一些例子。然后,他问我有什么函数指针的回报。我告诉他,这取决于函数指针的声明。

但我真的想知道函数指针的一些用法用C


解决方案

我觉得C中的典型例子是的的qsort 。从那里引用(我知道这是 http://www.cplusplus.com ,所以一个不very-好发部位,但它似乎是正确的)

 无效的qsort(无效*基地​​,为size_t NUM,为size_t大小,INT(*比较)(常量无效*,常量无效*));


  

这是比较两个元素的功能。该功能应遵循这个原型:


 内部比较器(常量无效* elem1,常量无效* elem2时);


  

函数必须接受两个参数是指向元素类型铸造为void *。这些参数应该转换回一些数据类型,进行比较。


  
  

该函数应该重新present elem1是否被视为小于,等于或大于elem2时更大的通过返回,分别为负值,零或正值的返回值



其他经典的例子就是计算器(例如参见,它的C ++,但在C相同)。

您有四个数学函数,例如:

 加浮动(浮动,浮动B){返回A + B; }
浮动减号(浮动,浮动B){返回A-B; }
乘浮动(浮动,浮动B){返回A * B; }
浮鸿沟(浮动,浮动B){返回A / B; }

在某种程度上,你选择你的操作

  / *这里应该有一个if或选择合适的操作*交换机/箱/
浮动(* ptrFunc)(浮动,浮动)=加;

和您以后可以称呼它(由于某种原因,你不想直接在如果/开关称之为,也许是因为你想其他的东西是通用的所有操作,例如记录或打印结果)

  float结果= ptrFunc(1.0F,2.0F);


另外,你可以使用函数指针的回调(如写的 vine'th )两件事情和穷人虚函数(你把它们放在一个结构,当你创建结构(你知道,喜欢用穷人的构造,但是这是C,所以我们将它称为一个初始化),为您节省会被那所使用哪些功能结构)。

Just recently I have been asked in an interview: Where can we use function pointers in C? And what function pointer returns. I said using we can call functions using function pointer then he asked some example but I could not satisfy with him some more example. He then asked me what function pointer return. I told him it depends upon the declaration of function pointers.

But I really would like to know some uses of function pointer in C.

解决方案

I think the classical example in C is qsort. Quoting from there (and I DO know it's http://www.cplusplus.com, so a not-very-good site, but it seems to be correct)

void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

Function that compares two elements. The function shall follow this prototype:

int comparator ( const void * elem1, const void * elem2 ); 

The function must accept two parameters that are pointers to elements, type-casted as void*. These parameters should be cast back to some data type and be compared.

The return value of this function should represent whether elem1 is considered less than, equal to, or greater than elem2 by returning, respectively, a negative value, zero or a positive value.


The other "classical" example is the calculator (for example see this, it's C++ but it's the same in C).

You have four math functions, for example

float Plus    (float a, float b) { return a+b; }
float Minus   (float a, float b) { return a-b; }
float Multiply(float a, float b) { return a*b; }
float Divide  (float a, float b) { return a/b; }

in some way you select your operation

/* Here there should be an if or a switch/case that selects the right operation */
float (*ptrFunc)(float, float) = Plus;

and you can call it later (for some reason you don't want to call it directly in the if/switch, perhaps because you want to make other "things" that are "common" to all the operations, like logging or printing the result)

float result = ptrFunc(1.0f, 2.0f);


Another two things you can use function pointers for are callbacks (as written by vine'th) and "poor man" virtual functions (you put them in a struct and when you create the struct (you know, like using a "poor man" constructor, but this is C so we will call it an initializer), you save there what functions will be used by that struct).

这篇关于我们在哪里可以使用C函数指针,有什么用函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆