是否有可能调用C函数,它的名字作为一个字符串? [英] Is it possible to call a C function, given its name as a string?

查看:99
本文介绍了是否有可能调用C函数,它的名字作为一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个问题在C难题之一!
这真的可能吗?

I saw this question in one of the C puzzles!! Is this really possible?

我怎么能调用一个函数,因为它的名字作为一个字符串?
是有可能使用所读取与scanf的可直接使用的字符串调用
功能?

How can I call a function, given its name as a string? is it possible to use string that is read with scanf be used directly to call a function?

我已经想到了,如果(STRCMP(STR字符串)),然后调用该函数。

i already have thought of if(strcmp(str,"string"))then call the function.

但没有任何其他的方法呢?

but is there any other approach?

推荐答案

由于没有提到什么是功能,也不是它的参数,我会想象它是这样的:

Since no mention of what is a function, nor its parameters, I would imagine it something like this:


typedef void (*foo)();
struct puzzleFoo{
   char *function_name;
   foo *fn;
};

创建基于结构的查找表,使用字符串参数

Create a lookup table based on the structure, using a string parameter


struct puzzleFoo *Lookup(const char *function_name);

然后通过阵列/列表寻找 puzzleFoo function_nam e时迭代,并执行函数指针叫 FN

Then iterate through an array/list looking for the puzzleFoo's function_name and execute the function pointer called fn.

这篇关于是否有可能调用C函数,它的名字作为一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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