这是我遇到的错误
mouse_cat.c:20: error: array type has incomplete element type
mouse_cat.c:20: error: expected ‘;’, ‘,’ or ‘)’ before numeric constant
mouse_cat.c:27: error: array type has incomplete element type
mouse_cat.c:27: error: expected ‘;’, ‘,’ or ‘)’ before numeric constant
这是源码
void enlever(char terrain [ ][ ],int x,int y)
{
terrain[y][x]=' ';
}
//********************************************//
void ajouter(char terrain [ ][ ],int x ,int y,int flag)
{
if(flag)
terrain[y][x]='C';
else
terrain[y][x]='S';
}
这是我的声明
#define x 23
#define y 22
char terrain [y][x];
我用的是 Gcc (linux)
最佳答案
定义宏的语法如下:
#define name replacer
编译的第一阶段,预处理器阶段处理所有所谓的预处理器指令(以#开头的行),包括这个。在这种情况下,它将所有出现的 name 替换为 replacer。因此,对于实际编译器,您的函数看起来像 void enlever(char** terrain,int 23, int 22)
。此外,您可能有变量名称,例如,其中包含字母 x 或 y。那些也会被替换。
为避免这种情况,编码标准建议使用大写字母命名使用#define 声明的常量。但这还不够,因为名称 X 或 Y 仍然可能作为变量或用户定义的数据类型的名称出现,甚至可能出现在字符串中。所以你可以使用类似的东西:
#define TERRAIN_LENGTH 23
#define TERRAIN_WIDTH 22
不要忘记使用常量而不是魔法数字是一个好习惯(比如在声明 int terrain[22][23];
中),因为它们使您的代码更容易理解并维护。
关于C代码编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19915195/