gpt4 book ai didi

c - 创建函数指针结构时指针类型不兼容

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

我正在尝试创建一个包含字符串和函数指针的结构数组,但是当我编译时,我收到一条警告,指出我已经从不兼容的指针类型进行了初始化。

我不知道为什么(对不起,如果我听起来很无知,我对 C 编程还很陌生)。

 typedef struct
{
char Player1[2], Player[2], **gameGrid;
int height,width;
int moveNum, player1Num, player2Num;
bool player1Win, player2Win;
}Game;

typedef int (*pointer_func)(Game *);

typedef struct
{
char *funcName;
pointer_func *f;
}userFunc;

int save(Game *struc);
int load(Game *struc);
int move(Game *struc);
int quit(Game *struc);
void free_grid(Game *struc);


int main(){
//initialised variables

userFunc Name_arr[] = {
{"save",save},
{"load",load},
{"quit",quit},
{"move",move}
};

被引用的四个函数如下:

int save(Game *struc)
{
char *str, *inputString, *writeString;
FILE *fp;
int nextPlayer;
int maxRead = 20;
bool DIRresponse;

while(true)
{
printf("Please provide a file name (20 characters max): ");
inputString = input_String(inputString, maxRead, stdin);

if((DIRresponse = check_Directory(inputString)) == true){
printf("That name already exists, choose another\n");
continue;
}
else
break;
}

if(struc->moveNum % 2 == 0)
nextPlayer = struc->player1Num;
else
nextPlayer = struc->player2Num;

sprintf(str,"%s.txt",inputString);
fp = fopen(str,"w");

sprintf(writeString, "%d %d %d %d %d", nextPlayer, struc->height,
struc->width, struc->moveNum, struc->moveNum);
fprintf(fp,writeString);

fclose(fp);

return 0;
}

int move(Game *struc)
{
return 1;
}

int load(Game *struc)
{
return 1;
}

int quit(Game *struc)
{
free_grid(struc);
exit(EXIT_SUCCESS);
}

最佳答案

你的指针级别不匹配:

typedef int (*pointer_func)(Game *); << Pointer type

typedef struct
{
char *funcName;
pointer_func *f; << Pointer to a pointer type.... OOPS
}userFunc;

制作 *f -> f 它应该可以工作。

关于c - 创建函数指针结构时指针类型不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48457126/

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