Puzzled over use of an Array of Struct

Hello Cplusplus forum user. I'm working on this program and it seem to be uncooperative for the life of me.

1
2
3
4
5
6
7
  typedef struct {
          T_contenu contenu;   
          int       numero;    
          } T_case;
  
  T_case T_ocean[HAUTEUR][LARGEUR];
/*T_contenu is an Enum of three integer ranging -1,0,1*/



1
2
3
4
5
6
7
8
9
10
11
12
void init_ocean(struct T_ocean[][LARGEUR])
{
int i=0,j=0;   
  for(i=0;i<=HAUTEUR;i++)
  {                        
    for(j=0;j<=LARGEUR;j++)
    {                
     T_ocean[i][j].contenu= -1;
     T_ocean[i][j].numero = -1;                  
    }
  }
}

Where T_ocean is an Array build with a Struct of an Enum and a regular int.
exemple:[enum1,enum2,enum3][random int value]

This function is just simply supposed to set both value of the "enum" and the "int" inside the array as "empty" yet it refuse to do so and give me an error.

The error is : expected primary-expression before '[' token . on line8
expected primary-expression before '[' token . on line9

I expect something like that to be due to syntax yet I can't find the bug. Could anyone enlighten me?

You declared T-ocean incorrectly in your function. It should read

 
void init_ocean(T_Case T_ocean[LARGEUR])


And this is IF you did not declare it as a global which is what I suspect from the first code but not entirely sure. I never seen struct used like that inside a function, it should be there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

struct T_Case
{
     T_contenu contenu;
     int numero;
}

void init_ocean(T_Case T_ocean[LARGEUR][HAUTER])
{
     for(int i=0 ; i < HAUTEUR ; i++)
     {
          for( int j=0 ; j < LARGEUR ; j ++)
          {
               T_ocean[i][j].contenu = -1;
               T_ocean[i][j].numero = -1;
          }
     }
}
Thank you for your help. Indeed the TypeDef was not global and putting it outside the main seem to have cleared the previous error message. Although, now, other kinds showed up.

1) Wouldn't Struct and T_case be equivalent since they are both the same type?

2) Removing the prototype void init_ocean(T_case T_ocean[HAUTEUR][LARGEUR]);
Seem to clear all error code. otherwise I get errors such as:
- variable or field `init_ocean' declared void
-`T_case' was not declared in this scope (although T_case is now global)
- previous declaration of `int init_ocean'
- conflicts with function declaration `void init_ocean(T_case (*)[10])'

Should I just remove the Prototype? Wouldn't that affect how the compiler compile the Function first then the rest of the main? I though prototype were mandatory?

P.s
I just realized why removing the prototype would clear my problem. I put the prototype before the Typedef making the compiler look for for something that came after.
Last edited on
Topic archived. No new replies allowed.