user2980564
user2980564

Reputation: 9

C code Compilation error

this is the error that i have

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

and this is the source code

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';
  }

and this is my declaration

#define x 23
#define y 22 

 char terrain [y][x]; 

i used Gcc (linux)

Upvotes: 0

Views: 85

Answers (2)

ZillGate
ZillGate

Reputation: 1183

You should change your code to:

#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';
  }

Issue 1: The function formal parameters are replaced with the marco definitions.

Issue 2: The second and subsequent dimensions of an array parameter must be given:

Also see: GCC: array type has incomplete element type

Upvotes: 1

Paul92
Paul92

Reputation: 9062

The define macro has the following syntax:

#define name replacer

The first stage in compilation, the preprocessor stage handles all so-called preprocessor directives (lines starting with #), including this. In this case, it replaces all occurences of name with replacer. So, your function will look like void enlever(char** terrain,int 23, int 22) to actual compiler. Also, you may have variable names, for example, which contain the letter x or y. Those would be replaced too.

To avoid such situations, a coding standard suggest naming constants declared with #define with capital letters. But this is not enough, because the name X or Y is still possible to arise as a name of a variable or user defined data type, or even in a string. So you may use something like:

#define TERRAIN_LENGTH 23
#define TERRAIN_WIDTH 22

Do not forget that is a good practice to use constants instead of magical numbers (like in declaration int terrain[22][23]; ), because they make your code much more easy to understand and mantain.

Upvotes: 2

Related Questions