呼叫在使用C字符串变量命名的功能 [英] Call a function named in a string variable in C

查看:92
本文介绍了呼叫在使用C字符串变量命名的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个变量来调用一个函数。这有可能用C?

I want to call a function using a variable. Is that possible in C?

其实,我想要做的是,从用户那里得到的功能名称,并将其存储在一个变量。现在我想调用已存储在其名称的功能。谁能告诉我如何可以在C做?

Actually, what I want to do is, get the function name from the user and store it in a variable. Now I want to call the function that has its name stored. Can anyone tell me how this can be done in C?

我想开发一个两个玩家的游戏一个AI游戏引擎。两个方案没有主要功能实现的逻辑赢得比赛将被输送到游戏引擎。
我要明确指出,该计划的名称将是相同的程序,实现逻辑赢得比赛primefunctions的。

I want to develop an AI game engine for a two player game. Two programs with no main function that implement the logic for winning the game will be fed to the game engine. Let me be clear that the program names will be same as that of the primefunctions in the program that implement the logic for winning the game.

因此​​,当用户输入的第一和第二玩家的名称,我可以将它们存储在2个不同的变量。现在,因为primefunction名称相同的程序名,我打算调用的函数与包含PROG变量名。

So when the user enters the name of first and second players, I can store them in 2 different variables. Now, since the primefunction names are same as that of the program names, I intend to call the functions with variables containing the prog names.

推荐答案

C不支持这种操作(即具有反射会语言)。你将能够做的最好的是创建函数名查找表函数指针并用它来找出该叫什么作用。或者你可以使用switch语句。

C does not support this kind of operation (languages that have reflection would). The best you're going to be able to do is to create a lookup table from function names to function pointers and use that to figure out what function to call. Or you could use a switch statement.

这篇关于呼叫在使用C字符串变量命名的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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