【问题标题】:C code Compilation errorC代码编译错误
【发布时间】:2013-11-23 18:38:22
【问题描述】:

这是我遇到的错误

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)

【问题讨论】:

  • 也许与 if(flag) { terrain[y][x]='C'; } else { 地形[y][x]='S';}
  • #define x 23 会造成很大的麻烦。任何你有 x 的地方,编译器都会把 23..
  • 那我该怎么办? !!!!!!!!!!!!!!!!!!

标签: c gcc


【解决方案1】:

define 宏的语法如下:

#define name replacer

编译的第一阶段,预处理器阶段处理所有所谓的预处理器指令(以#开头的行),包括这个。在这种情况下,它将所有出现的 name 替换为 replacer。因此,对于实际编译器,您的函数将类似于 void enlever(char** terrain,int 23, int 22)。此外,您可能有变量名称,例如,其中包含字母 x 或 y。那些也将被替换。

为了避免这种情况,编码标准建议用大写字母命名用#define 声明的常量。但这还不够,因为名称 XY 仍然可能作为变量或用户定义数据类型的名称出现,甚至在字符串中出现。所以你可以使用类似的东西:

#define TERRAIN_LENGTH 23
#define TERRAIN_WIDTH 22

不要忘记使用常量而不是神奇的数字是一个好习惯(例如在声明 int terrain[22][23]; 中),因为它们使您的代码更易于理解和维护。

【讨论】:

    【解决方案2】:

    您应该将代码更改为:

    #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:函数形式参数被宏定义替换。

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

    另见:GCC: array type has incomplete element type

    【讨论】:

    • @user2980564 你不应该#define x.. 这是合法的,但现在你不能再使用'x'了,因为它会被23取代..使用像#define TERRAIN_SZ_X 23这样的好名字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 2017-07-25
    • 1970-01-01
    相关资源
    最近更新 更多