我很難傳遞一個二維數組的結構。二維數組的大小是動態的(取決於給定的輸入參數)。我得到的錯誤:傳遞二維數組的結構
maze_array.c:76:14: error: incompatible types when assigning to type
‘BlockNode {aka struct BlockNode}’
from type‘BlockNode * {aka struct BlockNode *}’
Maze[i][j]=myBlockNode;
這裏是我的代碼:
int main(int argc, char *argv[]){
int MazeWidth=1;
int MazeHeight=1;
int NumOfAvatars=1;
BlockNode* Maze[MazeWidth][MazeHeight];
InitializeArray(Maze[0],MazeWidth,MazeHeight,NumOfAvatars);
return 1;
}
int InitializeArray(BlockNode** Maze,int MazeWidth, int MazeHeight, int NumOfAvatars){
for (int i=0; i<MazeWidth;i++)
{
for (int j=0; j<MazeHeight;j++)
{
//Initialize a BlockNode
printf("HERE1\n");
BlockNode *myBlockNode;
myBlockNode=calloc(1,sizeof(BlockNode));
myBlockNode->North=0;
myBlockNode->South=0;
myBlockNode->East=0;
myBlockNode->West=0;
int myArray[NumOfAvatars];
memset(myArray,0,sizeof(myArray));
memcpy(myBlockNode->AvatarVisited,myArray,sizeof(myArray));
//Place BlockNode in the Maze
Maze[i][j]=myBlockNode;
}
}
/*
printf("North %d\n", Maze[0][0]->North);
printf("AvatarVisted %d\n", Maze[0][0]->AvatarVisited[0]);
*/
return 1;
}
API變化到'INT InitializeArray(INT MazeWidth,INT MazeHeight,BlockNode *迷宮[MazeWidth] [MazeHeight],整數NumOfAvatars)',並調用作爲'InitializeArray(MazeWidth,MazeHeight,迷宮,NumOfAvatars);' – BLUEPIXY
即獲得了不工作,BLUEPIXY。 VLA不會以某種方式傳遞給任何其他數組的功能。 – Peter