我正在尝试将一个包含迷宫的 2D 数组传递给一个函数,该函数将用通过 xbee 接收的数据填充它并将新数组返回给我的主数组。
但是我收到一个错误:
../src/receiveMaze.c:132:13:错误:分配给具有数组类型的表达式
我的代码是:
int fillMaze(int maze[10][10]) {
while (x != 'x') {
for (row = 0; row < 10; row++) {
for (column = 0; column < 10; column++) {
maze[row][column] = 1;
}
}
row = 0;
column = 0;
while (row != 9 && column != 9) {
// if the receive buffer is not empty ->
if (xBee_receivedData()) {
// -> get next received data byte of the buffer
x = xBee_readByte();
a = (int) x + 0;
if (a == 1) {
if (row == 9) {
row = 0;
column++;
} else {
row++;
}
} else {
maze[row][column] = 0;
if (row == 9) {
row = 0;
column++;
} else {
row++;
}
}
} else {
printf("Error, did not receive data");
}
// Delay for the operation loop
}
}
return maze[10][10];
}
我正在我的主函数中调用该函数。
int main() {
// initialize the robot
bot_init();
// initialize spi-port
spi_init();
// initialize the display and the graphical operations
display_init();
gfx_init();
xBee_init();
int filledMaze[10][10];
filledMaze = fillMaze(filledMaze);
return 0;
}
您有多个问题:
-
return maze[10][10]
不会返回矩阵,它将返回一个从数组的越界索引中获取的单个int
值 -
int fillMaze(int maze[10][10])
表示该函数返回单个int
值,而不是矩阵(数组数组( -
首先
filledMaze = fillMaze(filledMaze)
错误,因为声明fillMaze
返回单个int
值 -
filledMaze = fillMaze(filledMaze)
也是错误的,因为filledMaze
是一个数组,你不能只分配给一个数组,只复制到它
要解决"返回 2d 数组"的问题,您首先应该意识到在这种情况下不需要它,因为fillMaze
函数已经填充了您作为参数传递的"2d 数组"。所以解决方案就是根本不返回任何东西:
// Fill in the passed "2d" array in-place, don't return anything
void fillMaze(int maze[10][10]) { ... }
// ...
fillMaze(filledMaze);