简体   繁体   English

C ++ 2D数组起作用-为什么第二个索引不能变量

[英]C++ 2D array to function - Why second index cannot be variable

Take this code. 采取此代码。

using namespace std;
int x = 10;

void test(int arr[][x]);

int main() {
   int mainArr[4][x];
   testFunc(mainArr);
}

void test(int arr[][x]) {
     ...
}

Now, this code doesn't work. 现在,此代码不起作用。 And I get an error message of note: candidate function not viable: no known conversion from 'int [4][10]' to 'int (*)[x]' for 1st argument void test(int arr[][x]);' 我收到一条错误消息note: candidate function not viable: no known conversion from 'int [4][10]' to 'int (*)[x]' for 1st argument void test(int arr[][x]);' . Now, I know that it would work if I replaced every time I saw and x with the number 10. That's not what I am asking though. 现在,我知道,如果我每次看到x都替换为10时,它将起作用,但这不是我要的。 I am asking why c++ will not allow we to put a variable into the function definition? 我问为什么C ++不允许我们将变量放入函数定义中? I feel that I am not understanding the way that c++ deals with global variables, 2d arrays and/or function definitions and would appreciate if someone could fill me in on what I am not understanding because I feel that the code that I have written above should be able to compile. 我觉得我不了解c ++处理全局变量,2d数组和/或函数定义的方式,如果有人可以填写我不了解的内容,我将不胜感激,因为我认为上面编写的代码应该可以能够编译。 Thanks. 谢谢。

When the array is passed to the function it is just passed a pointer to the beginning of the array. 当数组传递给函数时,它只是传递一个指向数组开头的指针。 If the function doesn't know the size of all the lower order dimensions, then it won't be able to calculate addresses for items in the array. 如果函数不知道所有低阶维度的大小,则它将无法计算数组中各项的地址。

If for example you had a reference in your function to arr[3][4] then if it knows the lower dimension is 10, it can do the calculation 3*10 + 4 to calculate the location of that item. 例如,如果您的函数中有一个对arr[3][4]的引用,那么如果知道较低的尺寸为10,则可以进行3 * 10 + 4的计算以计算该项目的位置。

Other types have additional data that comes with them and that can contain information like the size of each dimension, but that isn't the case for a C/C++ array. 其他类型具有附加的数据,这些数据可以包含诸如每个维的大小之类的信息,但C / C ++数组并非如此。

If you are wondering why it can't just use the value from the global, that is because it needs to know the value at compile time. 如果您想知道为什么它不能仅使用全局值,那是因为它需要在编译时知道该值。 If you change the declaration to: 如果将声明更改为:

const int x = 10;

Then it will be fine with your code since it knows that x is always 10, so it can take that into account at compile time. 然后,使用代码就可以了,因为它知道x始终为10,因此可以在编译时将其考虑在内。

C-style just don't allow that in C++. C风格只是不允许在C ++中使用。 C-style arrays must have constant dimension. C样式的数组必须具有恒定的尺寸。 Use of them is discouraged anyway. 无论如何,不​​鼓励使用它们。 They are mainly in the language as a historical hangover from C. 它们主要来自C语言的历史遗迹。

The name in C++ for an array whose size is not known at compile-time is vector . C ++中其大小在编译时未知的数组的名称为vector Your code could look like: 您的代码可能如下所示:

int x = 10;

void testFunc(vector<vector<int>> &x);

int main() 
{
    vector<vector<int>> mainArr{ 4, vector<int>{x} };
    testFunc(mainArr);
}

If the 4 does not need to change, you could use array<vector<int>, 4> instead, or make the 4 a template parameter. 如果4并不需要改变,你可以使用array<vector<int>, 4>代替,或使4模板参数。

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

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