传递二维数组作为参数 [英] Passing 2D array as argument

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

问题描述

在thsi例如:

  int类型的[2] [2] = {{1,2},{3,4}};
为int * p值=一个[0];
COUT<< P;
COUT&所述;&所述;&放大器;一个[0] [0];

两者提供相同的输出。
那么为什么我不能调用功能(说有趣)这样和遍历数组:

 乐趣(A [0]);乐趣为(int * P)
{
COUT&所述;&下,P [1] [1];
}


解决方案

 乐趣(A [0]); //这看起来OK无效乐趣为(int * P)//如果您添加的返回类型,这是确定
^^^^
{
   COUT&所述;&下,P [1] [1]; //不好!你不能在一个`为int *`2指数
   COUT<< P [1]; // OK,将打印[0] [1]
}

In thsi example:

int a[2][2]={{1,2},{3,4}};
int *p=a[0];
cout<<p;
cout<<&a[0][0];

Both gives the same output. Then why am I not able to call function (say fun)like this and loop through the array:

fun(a[0]);

fun(int *p)
{
cout<<p[1][1];
}

解决方案

fun(a[0]); //this looks OK

void fun(int *p) // this is OK if you add return type'
^^^^
{
   cout<<p[1][1]; //NOT OK!  You can't have 2 indices on an `int*`
   cout << p[1]; // OK, will print a[0][1]
}

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

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