功能指针优势 [英] Function pointer Advantage

查看:99
本文介绍了功能指针优势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


请让我知道函数指针的优势?

是否使用函数指针快速调用函数?

是否可以使用函数指针来优化代码?


谢谢和问候

Rajesh

解决方案

2003年10月29日星期三22:29:50 -0800,Rajesh写道:

亲爱的,

请让我知道函数指针的优点吗?
是否使用函数指针快速调用函数?
是否可以使用函数指针来优化代码?




如果没有函数指针,你就不能制作这样的泛型函数。

qsort可以对任何数组进行排序,只要你能给它一个比较函数

两个元素。

/ * base:数组的东西

nmemb:东西的数量

尺寸:东西的大小

比较:比较东西的功能* /

void qsort(void * base,size_t nmemb,size_t size,

int(* compar)(const void *,const void *));

- < br $>
NPV


大字打印,小字打印

Tom Waits - 向上走/>


ra****@tridentinfosol.com (Rajesh )在留言新闻中写道:< b2 ************************* @ posting.google.c om> ...
< blockquote class =post_quotes>请告诉我函数指针的优点?


用于表达某些设计

是否使用函数指针快速调用函数?


没有。可能比直接调用稍微慢一点

是否可以使用函数指针优化代码?




优化它是为了什么?


如果用来实现一个简单的解释器,函数poiters可以节省空间。

思考函数指针数组。


- -

Nick Keighley


ra **** @ tridentinfosol.com (Rajesh)在留言新闻中写道:< b2 ************************* @ posting.google.c om> ; ...

亲爱的,

请让我知道函数指针的优势?


函数指针允许您将函数作为参数传递给其他

函数(在qsort和bsearch上读取),实现函数查找

表(例如,根据名称或其他标准调用函数)等。

示例:


/ *

** h是一个函数的指针,它接受一个int参数并返回

int

* /

int foo(int x, int(* h)(int))

{

返回h(x);

}


int bar(int x)

{

返回x * 2;

}


int main(无效)

{

int y = foo(2,bar);

返回0;

}

是否使用函数指针快速调用函数?


不比直接调用函数快。

是否可以使用函数指针来优化代码?


取决于你所说的优化。可能不是。

感谢和问候
Rajesh



Dear All,

Please let me know the advantage of function pointer?
Is it fast calling function using function pointer?
Is it possible to use function pointer to optimise code?

Thanks and regards
Rajesh

解决方案

On Wed, 29 Oct 2003 22:29:50 -0800, Rajesh wrote:

Dear All,

Please let me know the advantage of function pointer?
Is it fast calling function using function pointer?
Is it possible to use function pointer to optimise code?



You couldn''t make a generic function like this without function pointers.
qsort can sort any array as long as you can give it a function to compare
two elements.
/* base : array of stuff
nmemb : amount of stuff
size : size of a stuff
compar : function to compare to stuffs */
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up


ra****@tridentinfosol.com (Rajesh) wrote in message news:<b2*************************@posting.google.c om>...

Please let me know the advantage of function pointer?
useful for expressing certain designs
Is it fast calling function using function pointer?
no. Likely slightly slower than a direct call
Is it possible to use function pointer to optimise code?



optimise it for what?

Function poiters could save space if used to implement a simple interpretor.
Think array of pointers to functions.

--
Nick Keighley


ra****@tridentinfosol.com (Rajesh) wrote in message news:<b2*************************@posting.google.c om>...

Dear All,

Please let me know the advantage of function pointer?
Function pointers allow you to pass functions as parameters to other
functions (read up on qsort and bsearch), implement function lookup
tables (e.g., call a function based on a name or other criteria), etc.
Example:

/*
** h is a pointer to a function taking one int parameter and returning
int
*/
int foo (int x, int (*h)(int))
{
return h(x);
}

int bar (int x)
{
return x * 2;
}

int main (void)
{
int y = foo (2, bar);
return 0;
}
Is it fast calling function using function pointer?
No faster than calling the function directly.
Is it possible to use function pointer to optimise code?
Depends on what you mean by "optimize". Probably not.

Thanks and regards
Rajesh



这篇关于功能指针优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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