如何将二维数组作为参数发送给函数 [英] how to send 2 dimension array as a parameter to a function

查看:97
本文介绍了如何将二维数组作为参数发送给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 [ 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


这篇关于如何将二维数组作为参数发送给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆