博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C/C++中二维数组作函数形参时,调用函数时,可传递的实参类型的小结
阅读量:2111 次
发布时间:2019-04-29

本文共 2527 字,大约阅读时间需要 8 分钟。

转自:

 

#include<iostream>

using namespace std;
int   fun(int a[][3],int n)     // 其中二维数组形参必须确定数组的第二维的长度,第一维长度可以不定
//int fun(int (*a)[3],int n)   //  和  int   fun(int a[][3],int n)  功能和用法完全相同
{
    int sum=0;
    for (int i=0;i<n;i++)
    {
         for(int j=0;j<3;j++)
         sum=sum+a[i][j];
    }
    return sum;
}
void fun1(int a[2])
{
    int n = sizeof(a); // 因为在实际函数调用时,传递来的实参都是指针,所有这里是对指针求sizeof,
                       // 而指针变量是int的整型变量。在vc++ 6.0中int类型变量占4字节,所以 n肯定是4
    printf("%d/n",n);
}
int fun2(int a[],int n)   //形参中可以不指定一维数组的大小。
{
    int sum=0;
    for(int i=0;i<n;i++)
         sum=sum+a[i];
    return sum;
}
int main()
{
    int b[2][3] = {
{1,2,3},{4,5,6}};
    int a[3]={1,2,3};
    int num=0;
    int num2=0;
    int i=2;
    int *p=&i;
        num=fun(&b[0],2);    //ok
    //  num =fun(b,2);  //ok
    //  num=fun(&b[0][0],2);  // error C2664: 'fun' : cannot convert parameter 1 from 'int *' to 'int [][3]'
    //  num=fun(&b,2);        // error C2664: 'fun' : cannot convert parameter 1 from 'int (*)[2][3]' to 'int [][3]'
    //  num=fun(b[0],2);      // error C2664: 'fun' : cannot convert parameter 1 from 'int [3]' to 'int [][3]'
   
        num2=fun2(&a[0],3); // ok
    //    num2=fun2(a,3);  //ok
    //  printf("%d/n",num2);
    //  printf("%d %d %d %d %d %d/n",b[0],&b[0],b[0][0],&b[0][0],b,&b); //只有b[0][0]是数组元素,其他均为地址
    //  结果: 1245032 1245032 1 1245032 1245032 1245032
    //  结论:虽然 b[0],&b[0],&b[0][0],b,&b 的值都相同,但是它们所代表(指向)的类型可能就不一样,其中&b[0]和b所指
    //  向的类型是相同的,b[0]和&b[0][0]的类型可以是认为是相当(注意是相当,不是相同)。
   //  C 语言中,当一维数组作为函数参数的时候,编译器总是把它解析成一个指向其首元素首地址的指针,
  //  当N维数组作为函数参数的时候,编译器总是把它解析成一个指向其首元素(这里的首元素是个N-1维数组)首地址的指针,
    //  printf( "%d/n ",num);
    //    printf("%d %d %d/n",sizeof(*p),sizeof(p),sizeof(b));  // sizeof(b)是求数组里所有元素所占的内存总和
    //  printf("%d %d %d %d/n",*p,p,sizeof(&b[0]),sizeof(b[0]));
          // sizeof(&b[0])是对指针,即数组b的第一个元素(当然它是个长度是3的一维数组)的地址作用
          // sizeof(b[0])是对数组b的第一个元素(当然它是个长度是3的一维数组)的作用,相当于求长度是3的一维数组所有
         // 元素所占内存和
    //  结果:2 1245008 4 12 //其中1245008是p的值,即*p的地址
    //    fun1(a); //ok
    //    fun1(&a[0]);//ok
    //    fun1(&a);// error C2664: 'fun1' : cannot convert parameter 1 from 'int (*)[3]' to 'int []'
        
    //    printf("%d/n",sizeof(a));
        return   0;
}
//*****************************************************************************
//二维数组的引用作形参时,注意事项。看例子
#include<iostream>
using namespace std;
   int   fun(int (&a)[2][3])         //ok,引用作形参,数组作引用形参,必须指定清楚所有维数
//
int  fun(int (&a)[ ][3],int n)     //error C2265: '<Unknown>' : reference to a zero-sized array is illegal
// i
nt  fun(int &a[2][3])             // error C2234: '<Unknown>' : arrays of references are illegal
{
    int sum=0;
    for (int i=0;i<2;i++)
    {
         for(int j=0;j<3;j++)
         sum=sum+a[i][j];
    }
    return sum;
}
int main()
{
    int b[2][3] = {
{1,2,3},{4,5,6}};
    int num=0;
    num=fun(b);
    printf("%d/n",num);
    return 0;
}

转载地址:http://asjef.baihongyu.com/

你可能感兴趣的文章
Leetcode C++《热题 Hot 100-29》22.括号生成
查看>>
Leetcode C++《热题 Hot 100-40》64.最小路径和
查看>>
Leetcode C++《热题 Hot 100-41》75.颜色分类
查看>>
Leetcode C++《热题 Hot 100-42》78.子集
查看>>
Leetcode C++《热题 Hot 100-43》94.二叉树的中序遍历
查看>>
Leetcode C++ 《第175场周赛-1 》5332.检查整数及其两倍数是否存在
查看>>
Leetcode C++ 《第175场周赛-2 》5333.制造字母异位词的最小步骤数
查看>>
Leetcode C++ 《第175场周赛-3》1348. 推文计数
查看>>
Leetcode C++《热题 Hot 100-44》102.二叉树的层次遍历
查看>>
Leetcode C++《热题 Hot 100-45》338.比特位计数
查看>>
读书摘要系列之《kubernetes权威指南·第四版》第一章:kubernetes入门
查看>>
Leetcode C++《热题 Hot 100-46》739.每日温度
查看>>
Leetcode C++《热题 Hot 100-47》236.二叉树的最近公共祖先
查看>>
Leetcode C++《热题 Hot 100-48》406.根据身高重建队列
查看>>
《kubernetes权威指南·第四版》第二章:kubernetes安装配置指南
查看>>
Leetcode C++《热题 Hot 100-49》399.除法求值
查看>>
Leetcode C++《热题 Hot 100-51》152. 乘积最大子序列
查看>>
[Kick Start 2020] Round A 1.Allocation
查看>>
Leetcode C++ 《第181场周赛-1》 5364. 按既定顺序创建目标数组
查看>>
Leetcode C++ 《第181场周赛-2》 1390. 四因数
查看>>