1
typedef struct piece Piece;
struct piece{
char color;
char symbol;
};
int readPiece(Piece * p[]);

// Because my code is quite long . I didn't put all of them here.

int main(void){
    int row = 0;
    int col = 0;

    Piece input[LEN][LEN];
    readPiece(input);
    return 0;
}

//read in 16*2 specific characters .

int readPiece(Piece * p[]){
    int row = 0;
    int col = 0;


    while(row < LEN){
       col = 0;
       while(col < LEN){
          scanf("%c%c",&(p[row][col] .color), &(p[row][col].symbol)); 
          if((p[row][col].color == 'R' || p[row][col].color == 'G' || p[row][col].color == 'B' || p[row][col].color == 'Y') && (p[row][col] . symbol == '*' || p[row][col].symbol == '^' ||  p[row][col].symbol == '#' || p[row][col].symbol == '$')){
             getchar();

          }else{
             return 0;
          }
          col ++;     
       }
       row ++;
    }
    return 1;

}

// I just start learning C language. I try to pass a 2d struct by pointer into a function but when I compile it , it shows 'incompatible pointer types passing'. So I wanna ask what's the right way to pass a 2d struct into a function and why mine is not working. Thanks.

Nina Liu
  • 33
  • 2
  • 2
    What you've provided looks fairly close to an MCVE ([MCVE]). It looks as though you need to define `LEN` and include ``, but that's about all. Providing more would not have been good. So well done on that. Why don't you use `int readPiece(Piece p[LEN][LEN]);`? It seems like a reasonable way to pass a 2D array of a structure type. (You don't use `row` or `col` in your reduced `main()`.) – Jonathan Leffler Jun 22 '17 at 04:13
  • Yeah I know int readPiece(Piece p[LEN][LEN]) will work but I am learning pointer right now. I just want to practice and I can't understand why this method is not working . Or should I use **p instead?Thx – Nina Liu Jun 22 '17 at 04:17
  • Just as you pass a 2d array in a function. https://stackoverflow.com/questions/16724368/how-to-pass-a-2d-array-by-pointer-in-c – Pushan Gupta Jun 22 '17 at 04:27
  • Well yeah I saw that. But I don't get it for the main function int main (int argc, char * argv[]) it works pretty well. I think my function code should work as well because they have similar prototype. – Nina Liu Jun 22 '17 at 04:36
  • The `int *p[]` notation denotes an array of pointers to `int`. What you've got in `main()` is not an array of pointers to `int`, but a 2D array of `int`. These are not the same; they're not even all that similar — but, just to make sure you get the maximum confusion out of it all, you use the same notation `array[index1][index2]` to access elements of both! – Jonathan Leffler Jun 22 '17 at 05:00

2 Answers2

2

I too came across this problem recently. Use Piece (*p)[LEN] in the function definition header (and the function declaration) instead of Piece *p[].

*p[] will be an array of pointers whereas (*p)[] will be pointer to array.

Try reading this as well:
C pointer to array/array of pointers disambiguation

J...S
  • 5,079
  • 1
  • 20
  • 35
  • 1
    Maybe "in the function prototype in both the declaration and the definition" would be precise, if a little wordy. – Jonathan Leffler Jun 22 '17 at 04:22
  • Using an array pointer as parameter is fine, but the syntax is a bit confusing. Better to use `Piece p [LEN][LEN]` which is equivalent but much more readable. – Lundin Jun 22 '17 at 08:12
0

Piece * p[] means an array of pointers so it is the wrong type.

The function should be declared as int readPiece(Piece p[LEN][LEN);.
Or if you prefer, int readPiece(size_t length, Piece p[length][length]);.

Lundin
  • 195,001
  • 40
  • 254
  • 396