C++ 将二维数组作为参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12990568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:50:49  来源:igfitidea点击:

Passing 2-D array as argument

c++pointers

提问by yogi

I am trying to pass a 2-d array to a function which accept a pointer to pointer. And I have learnt that a 2-d array is nothing a pointer to pointer(pointer to 1-D array). I when I compile the below code I got this error.

我试图将一个二维数组传递给一个接受指向指针的指针的函数。而且我了解到二维数组不是指向指针的指针(指向一维数组的指针)。当我编译下面的代码时,我收到了这个错误。

#include<iostream>

void myFuntion(int **array)
{
}
int main()
{
   int array[][]= {{1,2,3,4},{5,6,7,8,9},{10,11,12,13}};
   myFuntion(array);
   return 0;
}

In function 'int main()': Line 5: error: declaration of 'array' as multidimensional array must have bounds for all dimensions except the first compilation terminated due to -Wfatal-errors.

在函数“int main()”中:第 5 行:错误:将“数组”声明为多维数组必须具有所有维度的边界,但由于 -Wfatal 错误而终止的第一次编译除外。

Can anybody clear my doubt regarding this and some docs if possible for my more doubts.

如果可能的话,任何人都可以清除我对此的疑虑和一些文档,以消除我的更多疑虑。

采纳答案by vmp

  void myFunction(int arr[][4])

you can put any number in the first [] but the compiler will ignore it. When passing a vector as parameter you must specify all dimensions but the first one.

您可以在第一个 [] 中放置任何数字,但编译器将忽略它。将向量作为参数传递时,您必须指定除第一个维度之外的所有维度。

回答by Matthieu Brucher

Another templated solution would be:

另一个模板化解决方案是:

template<int M, int N>
void myFunction(int array[N][M])
{
}

回答by md5

You should at least specify the size of your second dimension.

您至少应该指定第二个维度的大小。

int array[][5] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 }, { 10, 11, 12, 13 } };

There is also an error which is often repeated. To pass a 2D array as argument, you have to use the following types:

还有一个经常重复的错误。要将二维数组作为参数传递,您必须使用以下类型:

void myFuntion(int (*array)[SIZE2]);
/* or */
void myFuntion(int array[SIZE1][SIZE2]);

回答by zabulus

Why don't use std::vector instead of "raw" arrays. Advantages:
1. It can dynamically grow.
2. There is no issues about passing arguments to the function. I.e. try to call void myFuntion(int array[SIZE1][SIZE2]); with array, that has some different sizes not SIZE1 and SIZE2

为什么不使用 std::vector 而不是“原始”数组。优点:
1. 可以动态增长。
2. 向函数传递参数没有问题。即尝试调用 void myFuntion(int array[SIZE1][SIZE2]); 使用数组,它有一些不同的大小而不是 SIZE1 和 SIZE2

回答by Fatima Zohra

#include<iostream>
 void myFuntion(int arr[3][4]);
int main()
  {
  int array[3][4]= {{1,2,3,4},{5,6,7,8},{10,11,12,13}};
 myFuntion(array);
  return 0;
 }
   void myFuntion(int arr[3][4])
   {

   }

http://liveworkspace.org/code/0ae51e7f931c39e4f54b1ca36441de4e

http://liveworkspace.org/code/0ae51e7f931c39e4f54b1ca36441de4e

回答by Omkant

declaration of ‘array' as multidimensional array must have bounds for all dimensions except the first So you have to give

将“数组”声明为多维数组必须对除第一个维度外的所有维度都具有边界 所以你必须给出

array[][size] //here you must to give size for 2nd or more 

For passing the array in function , array is not a pointer to a pointer but it's pointer to an array so you write like this

为了在函数中传递数组,数组不是指向指针的指针,而是指向数组的指针,因此您可以这样写

fun(int (*array)[])

Here if you miss the parenthesis around (*array) then it will be an array of pointers because of precedence of operators [] has higher precedence to *

在这里,如果您错过了 (*array) 周围的括号,那么它将是一个指针数组,因为运算符 [] 的优先级高于 *