将二维数组作为参数传递 [英] Passing 2-D array as argument
问题描述
我正在尝试将二维数组传递给一个接受指向指针的指针的函数.而且我了解到,二维数组并不是指向指针的指针(指向一维数组的指针).我在编译以下代码时遇到此错误.
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;
}
在函数'int main()'中: 第5行:错误:将数组"声明为多维数组时,除第一个数组外,所有维都必须具有边界 编译由于-Wfatal-errors而终止.
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.
如果可能的话,有人可以消除我对此和某些文档的怀疑.
Can anybody clear my doubt regarding this and some docs if possible for my more doubts.
推荐答案
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.
这篇关于将二维数组作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!