本文介绍了如何将二维数组作为参数发送给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法将二维数组发送给函数
i can not send 2 dimension array to a function
void fun(int **x); int _tmain(int argc, _TCHAR* argv[]) { int x[5][6]; for(i=0;i<5;i++) for(j=0;j<6;j++) x[i][j]=i+j; for(i=0;i<5;i++) for(j=0;j<6;j++) cout<<"x[i][j]="<<x[i][j]<<endl; fun(&(x);<-----Error return 0; } void fun(int **x) {. . .
. . .
推荐答案请参阅此 [ ^ ]和此 [^ ]来自 comp.lang.c [ ^ ]. 这两个问题的答案包含您所需要的. :) 顺便说一句:您的问题被标记为C ++而不是C.上面的链接将解决您C样式代码中的问题. 如果要将代码更改为C ++样式,则可以用替换二维整数数组 std::vector< std::vector<int> >(请参见 std :: vector [ ^ ]),然后将此矢量通过引用传递给您的函数. :) Please see this[^] and this[^] question from comp.lang.c[^]. The answers to these two questions contain all you need. :) BTW: Your question is tagged as C++ not C. Above links will solve the problem in your C - style code. If you want to change your code to C++ - style, then you can replace your two dimensional integer array with std::vector< std::vector<int> > (see std::vector[^]) and pass this vector by reference to your function. :)
您的函数应如下所示: Your function should be like this: void fun(int x[][6]) { }
并且您会这样称呼它:
and you would call it like this:
int x[5][6]; fun(x);
奇怪的是: Oddly enough: int _tmain(int argc, _TCHAR* argv[]) { // argv points to an array of c style strings. // a c style string is a character array where the // last character is set to '\x0'. // You had the required declaration for 2 dimensional arrays // right in front of you all the time. }
问候 Espen Harlinn
Regards Espen Harlinn
更多推荐
如何将二维数组作为参数发送给函数
发布评论