您现在的位置是:主页 > news > php网站开发设计模式/立即优化在哪里
php网站开发设计模式/立即优化在哪里
admin2025/6/21 9:14:32【news】
简介php网站开发设计模式,立即优化在哪里,北京最新疫情公布,免费软件app下载大全正能量网站求解一个关于typedef void (*msg)(void)的问题void hello(){printf("hello\n");}void bye(){printf("goodbye\n");}函数名是存在配置文件中的,如下:hhellobbye想要在main()中通过输入一个字母,并读取配置文件,来识别字母…
求解一个关于typedef void (*msg)(void)的问题
void hello()
{
printf("hello\n");
}
void bye()
{
printf("goodbye\n");
}
函数名是存在配置文件中的,
如下:
h=hello
b=bye
想要在main()中通过输入一个字母,并读取配置文件,来识别字母对应的函数名,并调用相应的函数。
想到可能会用到这个:
typedef void (*msg)(void)
但是具体不知道怎么写。
分享到:
------解决方案--------------------
文件中只能记录函数名称的字符串,还得通过字符串比较实现函数指针的赋值,要彻底解决你的问题,必须用脚本。
引用:Quote: 引用:这样的话,你需要用脚本来做,参考lua脚本与c的相互调用。
原来是这个样子的:
typedef void (*msg)(void);
void hello(void){printf("hello\n");}
void bye(void){printf("bye\n");}
void printX(int id)
{
msg words[3]={&hello,&bye};
msg m=map[id];
(*m)();
}
这样就可以通过printX(1),printX(2)来访问不同的函数。
现在hello和bye都是从文件中取了,就不知道怎么调了
------解决方案--------------------
typedef void (*msg)(void);
void hello(void){printf("hello\n");}
void bye(void){printf("bye\n");}
int getId(char ch)
{
if(ch == 'h')return 0;
else if(ch=='b')return 1;
return 2;
}
void printX(int id);
//这么处理行不
void printXbychar(char ch)
{
int id =getId(ch);
if(id!=2) printX(id);
}
void printX(int id)
{
msg words[3]={&hello,&bye};
msg m=map[id];
(*m)();
}
C++ 可以用 map<>,makepair 等处理
------解决方案--------------------
//函数名是存在配置文件k2fun.cfg中的,如下:
//h=hello
//b=bye
//想要在main()中通过输入一个字母,并读取配置文件,来识别字母对应的函数名,并调用相应的函数。
#include
#include
#define MAXFUNNAMELEN 30
#define MAXFUNS 100
void unknown() {
printf("unknown\n");
}
void hello() {
printf("hello\n");
}
void bye() {
printf("goodbye\n");
}
FILE *f;
typedef void (*pfun)(void);
pfun funs[MAXFUNS];
char funnames[MAXFUNS][MAXFUNNAMELEN];
int i,n,L;
char c;
char ln[1+1+MAXFUNNAMELEN+1+1];
int main() {
funs[0]=hello; strcpy(funnames[0],"hello");
funs[1]=bye; strcpy(funnames[1],"bye");
for (i=2;i
funs[i]=unknown; strcpy(funnames[i],"unknown");
}
f=fopen("k2fun.cfg","r");
if (NULL==f) {
printf("Can not open file k2fun.cfg!\n");
return 1;
}
printf("Input a char:");fflush(stdout);
rewind(stdin);
scanf("%c",&c);
n=0;
while (1) {
if (NULL==fgets(ln,1+1+MAXFUNNAMELEN+1+1,f)) break;
n++;
while (1) {
L=strlen(ln)-1;
if ('\n'==ln[L]
------解决方案--------------------
' '==ln[L]) ln[L]=0;
else break;
}
if ('='!=ln[1]) {
printf("Format Error at line %d:%s\n",n,ln);
} else {
if (c==ln[0]) {
for (i=0;i
if (0==strcmp(ln+2,funnames[i])) {
funs[i]();
fclose(f);