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.

5
  • 2
    What you've provided looks fairly close to an MCVE (minimal reproducible example). It looks as though you need to define LEN and include <stdio.h>, 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().) Commented Jun 22, 2017 at 4: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 Commented Jun 22, 2017 at 4:17
  • Just as you pass a 2d array in a function. stackoverflow.com/questions/16724368/… Commented Jun 22, 2017 at 4: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. Commented Jun 22, 2017 at 4: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! Commented Jun 22, 2017 at 5:00

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe "in the function prototype in both the declaration and the definition" would be precise, if a little wordy.
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.
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]);.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.