我正在尝试使用两个整数访问多维数组的值。该数组在另一个源文件中声明,我在问题源文件的顶部使用extern声明。
在声明我可以访问值的文件中,但是当我复制并粘贴相同的代码行时,编译器会通知我表达式必须是指向完整对象类型的指针。
我是C和指针的新手,所以我真的不知道发生了什么。我很感激任何想法。
extern struct piece board[8][8];
void move()
{
char row;
int column;
int rowNumber = 0;
printf("row: ");
scanf("%c",&row);
fflush(stdin);
printf("column: ");
scanf("%d",&column);
column--;
switch(row)
{
case 'A': rowNumber = 0; break;
case 'B': rowNumber = 1; break;
case 'C': rowNumber = 2; break;
case 'D': rowNumber = 3; break;
case 'E': rowNumber = 4; break;
case 'F': rowNumber = 5; break;
case 'G': rowNumber = 6; break;
case 'H': rowNumber = 7;
}
printf("\n your row is %d and column is %d",rowNumber,column);
struct piece temp = &board[rowNumber][column];
}
和数组本身:
struct piece board[8][8] = {{{0,0,blackRook,"Rook"},{0,1,blackKnight,"Knight"}, {0,2,blackBishop,"Bishop"},{0,3,blackQueen,"Queen"},{0,4,blackKing,"King"}, {0,5,blackBishop,"Bishop"},{0,6,blackKnight,"Knight"},{0,7,blackRook,"Rook"}},
{{1,0,blackPawn,"Pawn"}, {1,1,blackPawn,"Pawn"},{1,2,blackPawn,"Pawn"},{1,3,blackPawn,"Pawn"},{1,4,blackPawn,"Pawn"},{1,5,blackPawn,"Pawn"},{1,6,blackPawn,"Pawn"},{1,7,blackPawn,"Pawn"}},
{0},{0},{0},{0},
{{6,0,whitePawn,"Pawn"},{6,1,whitePawn,"Pawn"},{6,2,whitePawn,"Pawn"},{6,3,whitePawn,"Pawn"},{6,4,whitePawn,"Pawn"},{6,5,whitePawn,"Pawn"},{6,6,whitePawn,"Pawn"},{6,7,whitePawn,"Pawn"}},
{{7,0,whiteRook,"Rook"},{7,1,whiteKnight,"Knight"},{7,2,whiteBishop,"Bishop"},{7,3,whiteQueen,"Queen"},{7,4,whiteKing,"King"},{7,5,whiteBishop,"Bishop"},{7,6,whiteKnight,"Knight"},{7,7,whiteRook,"Rook"}}};
抱歉格式化,它在visual studio中显得更漂亮。前两个和后两个数组都填充了struct piece,其余的是0.空值似乎没有在我的代码中的任何其他地方引起任何问题。
编辑:这是作品定义
struct piece
{
int row;
int column;
int weight;
char name[20];
};
答案 0 :(得分:2)
除&board[r][c]
类型为struct piece *
而非struct piece
之外,您还需要包含定义piece
的头文件。编译器告诉您它没有关于struct piece
的足够信息。
答案 1 :(得分:1)
我想你的错误发生在这里:
struct piece temp = &board[rowNumber][column];
你忘记了* after after piece
struct piece* temp = &board[rowNumber][column];
应该有用。
您还应该包含定义片段结构的标题。
答案 2 :(得分:0)
以下行不正确。 temp根本不是指针变量。
struct piece temp = &board[rowNumber][column];
如果您只想要指向数组中一个元素的指针,那么
struct piece *temp = &board[rowNumber][column];
如果要为整个二维数组创建指针,那么它应该是
struct piece (*temp)[][8] = &board;
答案 3 :(得分:0)
仅靠外部是不够的,还必须在声明该结构的位置加上.h。