我遇到了返回多維數組的問題。我在一個函數中創建數組,然後在另一個函數中修改它。在主函數中,我想打印出如下所示的數組內容,但是,我沒有得到任何東西顯示在控制檯中。返回多維數組c
有什麼建議嗎?謝謝。
//Build the grid array given the number of rows, columns and levels
char ***buildGrid(int numRows, int numCols, int numLevels)
{
char ***levels;
levels = malloc(numLevels *sizeof(char *)); //Contains all levels
int rowIndex, levelIndex;
for (levelIndex = 0; levelIndex < numLevels; levelIndex++)
{
char **level = malloc(numRows * sizeof(char *)); //Contains all rows
for(rowIndex = 0; rowIndex < numRows; rowIndex++)
{
level[rowIndex] = malloc(numCols * sizeof(char)); //Contains all columns
}
levels[levelIndex] = level;
}
return levels;
}
void readGrid(char ***grid)
{
grid = buildGrid(3,3,3);
grid[0][0][0] = 'a';
}
int main (int argc, const char * argv[])
{
char ***gridData;
readGrid(gridData);
printf("%c", gridData[0][0][0]); //This does not output anything
return 0;
}
Gasp!當你到達***或****時,你可能沒有找到正確的方向! – 2011-04-02 16:35:26
它假設是一個3D數組。否則你會如何建議。 – David 2011-04-02 16:42:50