如何将二维数组作为参数发送给函数

编程入门 行业动态 更新时间:2024-10-24 18:22:22
本文介绍了如何将二维数组作为参数发送给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法将二维数组发送给函数

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

更多推荐

如何将二维数组作为参数发送给函数

本文发布于:2023-07-29 00:36:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   如何将   函数   发送给   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!