1

I'm writing a small battleship game in C. I have a player structure :

   struct player{ 
   char field[X][Y];
   ...
   }

And I have function print_field:

void print_field(char *field[X][Y]){
   for(int i = 0; i < X; i++){
       for(int j = 0; j < Y; j++){
           printf(" %c ", *field[i][j]);
       }
       printf("\n");
   }}

X and Y are defined, both equals to 5;

What is the right way to pass field to print_field in main?

int main(){
       struct player player1;    
       print_field(???);
       ...}

For now
print_field(&player1->field);
Gives me: warning incompatible pointer type [-Wincompatible-pointer-types]

shegeley
  • 85
  • 2
  • 8
  • Excuse me, @alk. I don't see any reference about accessing 2D array as a structure member in [How to pass 2D array (matrix) in a function in C?](https://stackoverflow.com/questions/40704345/passing-a-pointer-to-2d-char-array-as-a-structure-member-in-c?noredirect=1#comment68636696_40704345) – shegeley Nov 20 '16 at 14:07
  • Inside the caller access the array member as `player1.field`. On how to defined the parameter in `print_field` see the linked question's accepted answer. – alk Nov 20 '16 at 16:02
  • @alk if I use it like: 'print_field(player1.field);' It gives me 'incompatible pointer type [-Wincompatible-pointer-types]' – shegeley Nov 20 '16 at 20:01
  • Did you by any chance take a look at the answer I pointed you to? Use `void print_field(char field[X][Y])`. – alk Nov 21 '16 at 06:06
  • @alk I'm studying pointers now. I have all the game written without pointer, accessing structures like struct.member. Now I want to learn how to access structures and members with pointers. – shegeley Nov 21 '16 at 07:28

0 Answers0