简体   繁体   English

通过引用将指针传递到二维数组

[英]Passing a pointer to a 2-D array by reference

I just wanted to know--is this the correct way to pass a pointer to a 2-D Array by reference in a function? 我只是想知道-这是通过函数中的引用将指针传递到二维数组的正确方法吗?

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

My "theRow" and "theColumn" answers are very similar to the test function: 我的“ theRow”和“ theColumn”答案与测试功能非常相似:

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

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

In my main.cpp, I have a pointer to a 2d array and I called my functions like this: 在main.cpp中,我有一个指向2d数组的指针,并且这样调用了我的函数:

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

Would the array pass by reference or would I have to use a & instead of a *? 数组将通过引用传递,还是我必须使用&而不是*? I am just a little confused as I'm not entirely sure how 2-D arrays would work. 我有点困惑,因为我不确定2-D阵列如何工作。

Thanks. 谢谢。

This 这个

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

Or 要么

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

The first dimension in the array isn't needed. 不需要数组中的第一维。

Really, arrays are passed by reference by default. 实际上,默认情况下数组是通过引用传递的。 You don't need a & . 您不需要&

For more information, you may want to read this answer to this question . 有关更多信息,您可能需要阅读此问题的答案

C arrays are quite confusing and C++ inherited the confusion to be compatible with C. To understand how arrays work, the basics are: C数组非常令人困惑,并且C ++继承了与C兼容的困惑。要了解数组如何工作,基本知识如下:

1) array variables behave in expressions as a pointer to its first element except that it is constant (ie you cannot do "array = p;") and sizeof(array) will give you the size of the array (size of one element times the number of element) while sizeof(pointer) will give you the size of a pointer. 1)数组变量在表达式中的行为与其第一个元素的指针相同,只不过它是常量(即您不能执行“ array = p;”),而sizeof(array)将为您提供数组的大小(一个元素的大小的两倍)元素的数量),而sizeof(pointer)将为您提供指针的大小。

int array[5];
int* p = array;

Then the following expression are true: 那么以下表达式为真:

array[0] == *array
sizeof(array) == sizeof(int)*5
sizeof(p) == sizeof(void*)

2) When you define an argument of a function as an array, it is always passed as a pointer to the first element. 2)当您将函数的参数定义为数组时,它始终作为指向第一个元素的指针传递。 And behaves in the function as such. 并在功能上如此表现。 In fact, C ignore in that case the size of the passed array. 实际上,在这种情况下,C会忽略传递的数组的大小。 Even more, defining in a function an argument as an array or pointer to an element of the array is considered the same by the compiler. 甚至,编译器认为在函数中将参数定义为数组或指向数组元素的指针也是相同的。 So: 所以:

void func(int array[5]);
void func(int array[]);
void func(int *array);

declares the exact same function, and inside this function, sizeof(array)==sizeof(void*) 声明完全相同的函数,并且在此函数内,sizeof(array)== sizeof(void *)

Because of that, arrays seems to be always passed by reference, looking like a pointer to its first element. 因此,数组似乎总是通过引用传递的,就像指向其第一个元素的指针一样。

Now, multi dimensional arrays are just in fact single dimensional array whose elements are arrays. 现在,多维数组实际上就是一维数组,其元素是数组。 The confusing part in C/C++ about that is the confusing way C/C++ defines types in general. C / C ++中令人困惑的部分是C / C ++通常定义类型的混乱方式。 So: 所以:

test* array[9][9];

Remember that to read C/C++ type, you start from the identifier and that [] and () have precedence, so array is an array of 9 elements (first [9]) which are arrays of 9 elements (second [9]) which are pointers to type "test". 请记住,要读取C / C ++类型,请从标识符开始,并且[]和()具有优先级,因此array是9个元素(第一个[9])的数组,而9个元素(第二个[9])的数组这是键入“测试”的指针。

Now for the arr argument in this method: 现在,对于此方法中的arr参数:

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

arr is a pointer (parentheses change the precedence) to an array of 9 arrays of 9 "test" elements. arr是指向9个“测试”元素的9个数组的数组的指针(括号会更改优先级)。

This is very different from the previous "array" variable above, especially because "array" contains pointers to "test" while arr contains "test" element... 这与上面的先前“ array”变量有很大不同,特别是因为“ array”包含指向“ test”的指针,而arr包含“ test”元素……

BTW, the following declaration is completely identical: 顺便说一句,以下声明完全相同:

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

as "arr" can also be interpreted as a pointer to the first element of an array of 9 arrays to 9 "test"... as“ arr”也可以解释为指向9个数组的第一个元素的指针,指向9个“ test” ...

In practice, what you probably want to do is passing arrays of 9 arrays of "test", so: 实际上,您可能想要做的是传递9个“测试”数组的数组,因此:

boot sudoku::theRow(test arr[][9], int testNumber, int row)
{ ... }

test array[9][9];
sudoku::theRow(array, 0, 0);

And the method could also be defined as: 该方法也可以定义为:

boot sudoku::theRow(test (*arr)[9], int testNumber, int row)
{ ... }

A lot of information exists on the internet about this very confusing array/pointer mix-up of C/C++, for example: http://pw1.netcom.com/~tjensen/ptr/pointers.htm 互联网上有很多有关C / C ++的非常令人困惑的数组/指针混合的信息,例如: http : //pw1.netcom.com/~tjensen/ptr/pointers.htm

bool test::testTheNumber(test (*arr)[9][9], int testNumber, int row, int column)

is this the correct way to pass a pointer to a 2-D Array by reference in a function? 这是通过函数中的引用将指针传递到二维数组的正确方法吗?

Technically this is not passing the array by reference, but rather passing a pointer to the array by value. 从技术上讲,这不是通过引用传递数组,而是通过值传递指向数组的指针。 The semantics are quite similar otherwise. 否则语义非常相似。 To pass an array of 9x9 integers to the function by reference the signature would be: 通过引用将9x9整数数组传递给函数签名为:

bool test::testTheNumber(test (&arr)[9][9], int testNumber, int row, int column)

Note that the only difference is changing the * (pointer) to & (reference). 请注意,唯一的区别是将* (指针)更改为& (引用)。 The use of this would be: 使用此方法将是:

int main() {
   test array[9][9];
   test t;
   t.testTheNumber(array,1,2,3);
}

(Whether this does make sense or not is a completely different thing... you might want to provide a non-member function rather than a member function, for example) (这是否有意义完全不同……例如,您可能想提供非成员函数而不是成员函数)

Both of Geoff suggestions suggestion are equivalent and the problem they have is that in changing the interface the size of the array is not fixed in the signature. Geoff建议的两个建议都是等效的,它们存在的问题是,在更改接口时,数组的大小未在签名中固定。 That is, the function will take an array of 9x9, but it will also accept arrays of 2x9,3x9...Nx9. 也就是说,该函数将采用9x9的数组,但还将接受2x9,3x9 ... Nx9的数组。

bool f(int (*arr)[9]);
int main() {
    int array[9][9];
    f(array);          // ok, but so is:
    int array2[3][9];
    f(array2);
}

As you can see, by changing the type of the function argument, the first dimension is now free and the compiler will gladly accept any size there, so it is a worse option than the original one. 如您所见,通过更改函数参数的类型,第一个维度现在是自由的 ,并且编译器将很乐意在那里接受任何大小,因此,与原始版本相比,这是一个更糟糕的选择。

Finally, my suggestion would be creating a type that represents a 9x9 matrix and passing a reference to that type. 最后,我的建议是创建一个表示9x9矩阵的类型,并传递对该类型的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM