C代码编译错误

时间:2013-11-11 20:21:33

标签: c gcc

这是我的错误

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)

2 个答案:

答案 0 :(得分:2)

define宏具有以下语法:

#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];)是一个很好的做法,因为它们使您的代码更容易理解和维护。

答案 1 :(得分:1)

您应该将代码更改为:

#define TX 23
#define TY 22 

void enlever(char terrain [ ][TY],int x,int y)
 {
    terrain[y][x]=' ';
  }

//********************************************//

 void ajouter(char terrain [ ][TY],int x ,int y,int flag)
  {
   if(flag) 
    terrain[y][x]='C';
   else
    terrain[y][x]='S';
  }

问题1:函数形式参数被marco定义替换。

问题2:必须给出数组参数的第二个和后续维度:

另见:GCC: array type has incomplete element type