gpt4 book ai didi

c++ - 通过引用传递指向二维数组的指针

转载 作者:行者123 更新时间:2023-11-27 23:21:13 24 4
gpt4 key购买 nike

我只是想知道——这是在函数中通过引用传递指向二维数组的指针的正确方法吗?

bool test::testTheNumber(test (*arr)[9][9], int testNumber, int row, int column)
{
if(theRow(arr, testNumber, row) && theColumn(arr, testNumber, column))
....
}

我的“theRow”和“theColumn”答案与测试函数非常相似:

bool sodoku::theRow(sodoku (*arr)[9][9], int testNumber, int row)

bool sodoku::theColumn(sodoku (*arr)[9][9], int testNumber, int column)

在我的 main.cpp 中,我有一个指向二维数组的指针,我这样调用我的函数:

test *arr[9][9];
theRow(arr,0,0);
theColumn(arr,0,0);
testTheNumber(arr,0,0,0,0);

数组是按引用传递还是我必须使用 & 而不是 *?我只是有点困惑,因为我不完全确定二维数组是如何工作的。

谢谢。

最佳答案

这个

bool test::testTheNumber(test (*arr)[9], int testNumber, int row, int column)
{
if(theRow(arr, testNumber, row) && theColumn(arr, testNumber, column))
....
}

或者

bool test::testTheNumber(test arr[][9], int testNumber, int row, int column)
{
if(theRow(arr, testNumber, row) && theColumn(arr, testNumber, column))
....
}

不需要数组中的第一个维度。

实际上,默认情况下数组是通过引用传递的。您不需要 &

有关更多信息,您可能需要阅读 this answer to this question .

关于c++ - 通过引用传递指向二维数组的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12878281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com