在C中调用函数 [英] calling function in C

查看:70
本文介绍了在C中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



让我说我有10个功能命名 -


display1()

display2()

display3()

display4()

......

......

display10()


假设已经定义了所有这些函数。现在我们必须修改程序,以便通过
输入任何函数名称来调用
键盘。例如,如果我们从键盘接受字符串

display5,则应调用display5()方法。

流程应该是通用的,这样代码就不需要修改了

虽然增加了一些功能。


如果有人有任何想法,请告诉我。


问候

Shiv

Hi,
Lets say I have some 10 functions named -

display1()
display2()
display3()
display4()
......
......
display10()

Assume that all these functions have been defined. Now we have to
modify the program so that whatever function name is entered by
keyboard will be invoked. For example if we are accepting string
display5 from the keyboard then display5() method should be invoked.
The flow should be generic so that the code need not be modified even
though some more functions are added.

If any one has any idea do let me know.

Regards
Shiv

推荐答案

Shiv Ranjan说:
Shiv Ranjan said:



让我说我有10个功能命名 -


display1()

display2()

display3()

display4()

.....

.....

display10()


假设已经定义了所有这些功能。现在我们必须修改程序,以便通过
输入任何函数名称来调用
键盘。
Hi,
Lets say I have some 10 functions named -

display1()
display2()
display3()
display4()
.....
.....
display10()

Assume that all these functions have been defined. Now we have to
modify the program so that whatever function name is entered by
keyboard will be invoked.



构造一个查找表(例如哈希表,二进制搜索树或类似的东西),其条目是包含密钥的结构(

函数的名称,表示为字符串)和有效负载(指向函数的指针

与该字符串相关联)。是的,每当你需要重新编译时,你需要重新编译
在表格中添加一个新条目。


-

Richard Heathfield< ; http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Construct a lookup table (e.g. hash table, binary search tree, or something
like that) whose entries are structures containing a key (the name of the
function, expressed as a string) and a payload (a pointer to the function
associated with that string). Yes, you will have to recompile whenever you
add a new entry to the table.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Richard Heathfield写道:
Richard Heathfield wrote:

Shiv Ranjan说:
Shiv Ranjan said:

>
让我说我有10个函数命名 -

display1()
display2()
display3 ()
display4()
.....
.....
display10()

假设已经定义了所有这些功能。现在我们必须修改程序,以便调用
键盘输入的任何函数名称。
>Hi,
Lets say I have some 10 functions named -

display1()
display2()
display3()
display4()
.....
.....
display10()

Assume that all these functions have been defined. Now we have to
modify the program so that whatever function name is entered by
keyboard will be invoked.



构造一个查找表(例如哈希表,二进制搜索树,或者像这样的


Construct a lookup table (e.g. hash table, binary search tree, or
something like that)



我认为这些任务对于这项任务来说都是过度的,并且可能比主要任务给出更多的b / b
麻烦。


简单的线性搜索应该没问题。特别是因为看起来每键盘输入会有一次搜索。


-

Bartc

Any of these will be overkill for this task I think, and likely to give more
trouble than the main assignment.

A simple linear search should be fine. Especially as it seems there will be
one search per keyboard entry.

--
Bartc


2008年5月28日星期三上午10:20:29 +0000,Richard Heathfield写道:
On Wed, May 28, 2008 at 10:20:29AM +0000, Richard Heathfield wrote:

Shiv Ranjan说:
Shiv Ranjan said:



假设我有10个函数命名 -


display1()
Hi,
Lets say I have some 10 functions named -

display1()



[snip]

[snip]


display10()


假设已经定义了所有这些功能。现在我们必须修改程序,以便通过
输入任何函数名称来调用
键盘。
display10()

Assume that all these functions have been defined. Now we have to
modify the program so that whatever function name is entered by
keyboard will be invoked.



构造一个查找表(例如哈希表,二进制搜索树或类似的东西),其条目是包含密钥的结构(

函数的名称,表示为字符串)和有效负载(指向函数的指针

与该字符串相关联)。是的,每当你需要重新编译时,你需要重新编译
在表格中添加一个新条目。


-

Richard Heathfield< ; HTTP://www.cpax.org.uk>


Construct a lookup table (e.g. hash table, binary search tree, or something
like that) whose entries are structures containing a key (the name of the
function, expressed as a string) and a payload (a pointer to the function
associated with that string). Yes, you will have to recompile whenever you
add a new entry to the table.

--
Richard Heathfield <http://www.cpax.org.uk>



(你= OP)

理查德解决方案的好处是你不必重新编译

访问查找表的部分,只是构建它的部分。

尽管如此,每当你用C编写函数时,你都要编译它

迟早要完成这项工作。表格本身也必须重新编译,

因为在标准C中你无法通过函数的名称发现函数指针。


你可能想要将应用程序的可移植性限制为

系统,如果在共享库中定义了函数,则系统特定的方法来获取函数指针

来自字符串

(如unices上的dlsym()。)


Szabolcs

(you=OP)
The nice thing with Richard''s solution is that you will not have to recompile
the part that accesses the lookup table, just the part that builds it.
Nonetheless, whenever you write a function in C, you''ll have to compile it
sooner or later to get that work. The table itself has to be recomplied too,
since in standard C you cannot spot a function pointer by the function''s name.

You may want to restrict the portability of your application to
systems where there is a system-specific way to get a function pointer
from a string if the funtion is defined in a shared library
(such as dlsym() on unices.)

Szabolcs


这篇关于在C中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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