当函数 C++ 调用时,类 2D 数组将变为 arr []
Class 2D array turns to arr [] when called by function C++
我有一个大学作业,我对如何正确传递数组以防止数组作为单个数组而不是 2D 数组传递感到非常困惑。
我们将创建一个随机迷宫生成器,让我们也可以玩那个迷宫。我们正在使用专门的窗口代码来显示迷宫,但这不是问题所在,所以我把它排除在外我的讲师给了我们工作框架代码。我必须更改什么才能使其正常工作?
我们还没有学习动态内存位置或向量。我们必须使用数组。请帮忙!?
这是他的代码:我使用了相同的,只是添加了所有函数参数。不过我没有用"迷宫"改变任何东西
class MazeSquare
{
public:
bool leftWall, rightWall, bottomWall, topWall;
bool visited;
int steps;
MazeSquare() // constructor
{
Initialise();
}
void Initialise(void) // reinitialise a square for a new maze
{
leftWall = true; // create the maze square with all the walls
rightWall = true;
bottomWall = true;
topWall = true;
visited = false; // the robot has not visited the square yet
steps = 256; // greater than maximum possible number of steps
}
};
// constants
const int MAZE_SIZE = 16;
// function prototypes
void CreateMaze(MazeSquare maze[MAZE_SIZE][MAZE_SIZE]);
void SolveMaze(MazeSquare maze[MAZE_SIZE][MAZE_SIZE]);
void RestartMaze(MazeSquare maze[MAZE_SIZE][MAZE_SIZE]);
void MoveRobot(MazeSquare maze[MAZE_SIZE][MAZE_SIZE], int &x, int &y, Point click);
void DrawWindow(MazeSquare maze[MAZE_SIZE][MAZE_SIZE], int x, int y);
int ccc_win_main() // main function for a graphics program
{
MazeSquare maze[MAZE_SIZE][MAZE_SIZE]; // maze design
int x = 0, y = 0; // robot position
bool exit = false; // flag to control end of program
// initialise the random number generator
srand((unsigned int)(time(NULL)));
/* initialise the window coordinates here */
CreateMaze(maze); // create a new maze
DrawWindow(maze); // draw the image in the GUI window
do
{
// get a mouse click
Point click = cwin.get_mouse("Click a button or move the robot");
// handle the different types of mouse clicks
if (/* new button is clicked */)
{
CreateMaze(maze);
x = 0;
y = 0;
}
if (/* solve button is clicked */)
{
SolveMaze(maze);
}
if (/* restart button is clicked */)
{
RestartMaze(maze);
x = 0;
y = 0;
}
if (/* exit button is clicked */)
{
exit = true;
}
// handle robot moves
if (/* maze is clicked */)
{
MoveRobot(maze, x, y);
}
DrawWindow(maze);
} while (!exit);
return 0;
}
困惑一 - 不能将数组传递给 C++ 中的函数。
困惑二 - 不能在C++中将数组声明为函数参数
困惑之三 - 2D 数组是单个数组,2D 数组是数组的数组,因此它也是一个"单个数组"。我想我是说术语单个数组没有多大意义。
数组在C++是一个令人困惑的话题。你不能做你期望能够用它们做的所有事情。相反,一切都是用指针完成的。C++数组和指针之间的关系是另一个令人困惑的话题。你真的需要读一本书。任何具体问题,再问一遍。
但从好的方面来说,我认为你的代码没有任何特别的问题。你肯定不会像你担心的那样将单个数组传递给你的函数。
编辑:
也许我应该更清楚地说明这一点。关于第二点,这段代码
void CreateMaze(MazeSquare maze[MAZE_SIZE][MAZE_SIZE]);
当然看起来您正在声明一个带有数组参数的函数。但事实并非如此。相反,编译器获取代码并将其转换为使用指针的等效代码。
关于第一点,这段代码
CreateMaze(maze); // create a new maze
当然看起来你正在将数组传递给函数,但你又不是。给定代码,编译器传递指向迷宫数组的第一个元素的指针,它不会传递数组本身。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 为什么存储在数组中的地址值与其自己的地址相同,它应该指向某个不同的地址(&arr[0])?
- 在 C/C++ 中将数组作为形式参数作为 int arr[] 和 int arr[N] 传递的区别
- 澄清了 strcpy() 在像这样初始化字符数组时的工作 *Arr.
- 声明数组>> int arr[] 时出错;在C++(虽然在 Java 中有效)?
- 数组问题:变量周围的堆栈'arr'已损坏
- C 将1D动态数组施放到2D数组以使用常规索引(例如ARR [i] [J])
- 访问像 arr[1,2,3] 这样的一维数组C++ - 这是什么意思
- 可以在C++中声明这样的数组吗:`int arr[3/0]`
- 在c++中,取一个整数数组(arr[])、其长度(N)和元素数右移(M):
- 当函数 C++ 调用时,类 2D 数组将变为 arr []
- 为什么引用数组的语法需要(&arr)而不是(arr&)。另外,它如何帮助绑定检查?
- 在c++中声明一个大小为n的bool数组,保证每个arr[n]都为0
- 创建大小为 int arr[1000000][1000000] 的大型 2D 数组
- 为什么编译器不在参数中传递数组字符 *arr[] 的大小?
- 数组溢出c++ (at arr[0])