有趣的问题动态形成功能名称? [英] Interesting problem dynamically forming func names ??

查看:84
本文介绍了有趣的问题动态形成功能名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过C编程中的函数调用

语言?


我面临一个有趣的问题,我需要采取函数

名称和参数在飞行中(实际上从数据库中作为字符串)和表单

a函数调用基于此?

类似于FUN_NAME =" ; FUN1" ,ARGS =" arg1,arg2,arg3" ,对于这个

对函数调用必须是

fun1(arg1,arg2,arg3);


假设fun1(int ,char,char *)是在电话会议之前定义的吗?


ie,


get fun_name,args;

打个电话


问候,

Onkar


Is it possible to from function call on fly in C programming
language ?

I am faced with an interesting problem in that I need to take function
name and arguments on fly (actually from Database as string ) and form
a function call based on that ?
something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg3" , for this
pair function call must be
fun1(arg1,arg2,arg3);

assume that fun1(int,char,char*) is defined before the call ?

i.e.,

get fun_name,args;
form a call

Regards,
Onkar



推荐答案

2008年6月20日9:29, *** **********@gmail.com 写道:
On 20 Jun 2008 at 9:29, on*************@gmail.com wrote:

是否有可能在C编程中从函数调用中获取

语言?


类似FUN_NAME =" fun1" ,ARGS =" arg1,arg2,arg3" ,对于这个

对函数调用必须是

fun1(arg1,arg2,arg3);
Is it possible to from function call on fly in C programming
language ?

something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg3" , for this
pair function call must be
fun1(arg1,arg2,arg3);



主要选择是:维护表格中的函数指针和哈希值
FUN_NAME到表中的索引,或使用像dlsym()到

访问符号表本身。

The main choices are: maintain a table of function pointers and hash
FUN_NAME to an index in the table, or use a function like dlsym() to
access the symbol table itself.


6月20日下午2:29,onkar.n。 maha ... @ gmail.com写道:
On Jun 20, 2:29 pm, onkar.n.maha...@gmail.com wrote:

是否可以通过C编程中的函数调用

语言?


我面临一个有趣的问题,我需要在函数中使用函数

名称和参数(实际上是从数据库中作为字符串)和形式
基于此的
a函数调用?


类似FUN_NAME =" fun1" ,ARGS =" arg1,arg2,arg3" ,对于这个

对函数调用必须是

fun1(arg1,arg2,arg3);


假设fun1(int ,char,char *)是在电话会议之前定义的吗?


ie,


get fun_name,args;

打个电话


问候,

Onkar
Is it possible to from function call on fly in C programming
language ?

I am faced with an interesting problem in that I need to take function
name and arguments on fly (actually from Database as string ) and form
a function call based on that ?

something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg3" , for this
pair function call must be
fun1(arg1,arg2,arg3);

assume that fun1(int,char,char*) is defined before the call ?

i.e.,

get fun_name,args;
form a call

Regards,
Onkar



/ *未编译* /

#include< stdio.h>

#include< stdlib.h>


#define NAME_LEN ..

#define NUM ..

struct _node {

char name [NAME_LEN];

void( * fnPtr)(void);

}节点;


int foo(int a);

void bar(char * a,浮b);

double baz(无效);


int

main(无效){

struct node table [NUM] = {{" foo",& foo},{" bar"& bar},{" baz",

& baz}};

char name [NAME_LEN] = getNameFromDatabase();

int count = 0;

for(; count< NUM ;计数++) {

if(strcmp(name,table.name)== 0){

table.fnPtr(/ *放置参数* /);

}

}

返回0;

}

这是一种非常粗略的方法。你可以对此进行改进(使用链接列表

或哈希表代替数组,为参数做些事情等)

但基本框架将是相同的。


C中没有任何泛型函数指针(void *只能代表对象的
)但指向函数的指针可以代表任何其他

功能类型。您可以使用一些转换来关闭编译器。

/* Not compiled */
#include <stdio.h>
#include <stdlib.h>

#define NAME_LEN ..
#define NUM ..

struct _node {
char name[NAME_LEN];
void (*fnPtr)(void);
}node;

int foo(int a);
void bar(char *a, float b);
double baz(void );

int
main(void) {
struct node table[NUM] = { {"foo", &foo}, {"bar", &bar}, {"baz",
&baz} };
char name[NAME_LEN] = getNameFromDatabase();
int count = 0;
for ( ; count < NUM; count++) {
if (strcmp(name, table.name) == 0) {
table.fnPtr(/*place the arguments */);
}
}
return 0;
}
This is a very crude approach. You can refine on this( use link list
or hash tables in place of arrays, do something for parameters etc)
but the basic framework is going to be the same.

There aren''t any generic function pointers in C (void * can stand only
for objects) but a pointer to a function can stand for any other
function type. You can use some casting to shut the compiler up.




< on ************* @ gmail.com写信息

新闻:6f ********************************* * @ k37g2000 hsf.googlegroups.com ...

<on*************@gmail.comwrote in message
news:6f**********************************@k37g2000 hsf.googlegroups.com...

是否可以通过C编程中的函数调用

语言?


我面临一个有趣的问题,我需要在函数中使用函数

名称和参数(实际上是从数据库中作为字符串)和表格
基于此的
a函数调用?


类似FUN_NAME =" fun1" ,ARGS =" arg1,arg2,arg3" ,对于这个

对函数调用必须是

fun1(arg1,arg2,arg3);


假设fun1(int ,char,char *)是在通话之前定义的吗?
Is it possible to from function call on fly in C programming
language ?

I am faced with an interesting problem in that I need to take function
name and arguments on fly (actually from Database as string ) and form
a function call based on that ?
something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg3" , for this
pair function call must be
fun1(arg1,arg2,arg3);

assume that fun1(int,char,char*) is defined before the call ?



已经提到了一个功能名称和地址表。


如果您使用的是Windows,并且功能在DLL文件,然后它们通常按名称访问



GetProcAddress(hinst," ijlInit")


也许你的系统可能有类似的东西。


如果函数可以有不同的参数集和返回值,那么'/ b $ b'是额外的并发症;你需要根据他们的签名对他们进行分类,并分别给每个小组打电话。


我们谈论了多少功能?函数名称不能是任意的,因为它必须已经存在于某个地方(在大多数语言中不是

只是C)。


- -

Bartc

A table of function names and addresses has been mentioned.

If you are using Windows and the functions are in a DLL file, then they are
often accessed by name anyway:

GetProcAddress(hinst,"ijlInit")

and perhaps there might be something similar on your system.

If the functions could have different parameter sets and return values,
that''s an extra complication; you will need to classify them according to
their signatures and call each group separately.

How many functions are we talking about? The function name cannot be
arbitrary because it must already exist somewhere (in most languages not
just C).

--
Bartc


这篇关于有趣的问题动态形成功能名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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