当我这样做时,我得到以下错误
"错误:'mscp_commands'的存储大小未知"
struct command mscp_commands[]; /* forward declaration */
Run Code Online (Sandbox Code Playgroud)
后来我有:
struct command mscp_commands[] = {
{ "help", cmd_help, "show this list of commands" },
{ "bd", cmd_bd, "display board" },
{ "ls", cmd_list_moves, "list moves" },
{ "new", cmd_new, "new game" },
{ "go", cmd_go, "computer starts playing" },
{ "test", cmd_test, "search (depth)" },
{ "quit", cmd_quit, "leave chess program" },
{ "sd", cmd_set_depth, "set maximum search depth (plies)" },
{ "both", cmd_both, "computer plays both sides" },
};
Run Code Online (Sandbox Code Playgroud)
前面以这种方式声明struct mscp_commands有什么问题?
命令结构先前已定义:
struct command {
char *name;
void (*cmd)(char*);
char *help;
};
Run Code Online (Sandbox Code Playgroud)
struct command mscp_commands[];定义不是声明(假设struct command已定义),但它不知道该点的存储大小,因为不知道元素的数量mscp_commands.这[]是明显不同的案例之一*.
你可以写:
extern struct command mscp_commands[];
Run Code Online (Sandbox Code Playgroud)
这确实是一个宣言.