gpt4 book ai didi

c - 如何将字符串转换为 C 中的函数指针?

转载 作者:太空宇宙 更新时间:2023-11-04 00:42:55 24 4
gpt4 key购买 nike

<分区>

Possible Duplicates:
call a function named in a string variable in c
Dynamically invoking any function by passing function name as string

我有某些功能..比如说,

double fA1B1C1( .. ) {
...
}

double fA2B2C3( .. ) {
...
}

double (*fptr)( .. );

fptr myfunc;

这些函数已经定义好了。

所以当用户输入字符串“A2B2C2”时,它被放入一个变量并转换为“fA2B2C2”

myfunc = formatted_string; // DOES NOT WORK

但是上面的语法不起作用。即使对其进行类型转换也不起作用。

myfunc = (fptr) formatted_string // DOES NOT WORK

但是如果我硬编码为

myfunc = fA2B2C2(或)myfunc = &fA2B2C2,它工作得很好。

我哪里错了?

编辑::

I tried creating a hash table and a lookup function. Like

create_hashEntry(hashtable, string, formatted_string);
// string = "A1B1C1; formatted_string = "fA1B1C1"

然后是查找函数

myfunc lookup_func(char *string) {
return(get_hashEntry(hahstable, string));
}

这也失败了。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com