二维数组作为函数的实参以及定义函数形参的方法是什么

发布时间:2021-10-14 10:47:42 作者:柒染
来源:亿速云 阅读:277

今天就跟大家聊聊有关二维数组作为函数的实参以及定义函数形参的方法是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

#include <iostream>
#include <iomanip>
using namespace std;

//////////method first//////////////////
//直接用二维数组的形式
void fun(int a[3][4])
{
    for(int i=0; i<3; ++i)
    {
        for(int j=0; j<4; ++j)
        {
            cout<<"a["<<i<<"]["<<j<<"]="<<setw(3)<<a[i][j]<<"\t";
        }
        cout<<endl;
    }
}

/////////////method second//////////////
//用指向数组的指针
void fun1(int (*p)[4])
{
    for(int i=0; i<3; ++i)
    {
        for(int j=0; j<4; ++j)
        {
            cout<<"p["<<i<<"]["<<j<<"]="<<setw(3)<<p[i][j]<<"\t";
        }
        cout<<endl;
    }
}

///////////method third/////////////////
//形参采用指针,如int* p,而主函数进行特殊操作,使二维数组传值到函数里。
void fun2(int *p)
{
    for(int i=0; i<4; ++i)
    {
        cout<<"p["<<i<<"]="<<setw(3)<<p[i]<<"\t";
    }
    cout<<endl;
}
////////////method forth////////////////
//用指向指针的指针,如int** p,
void fun3(int **p)
{
    for(int i=0; i<3; ++i)
    {
        for(int j=0; j<4; ++j)
        {
            cout<<"p["<<i<<"]["<<j<<"] = "<<setw(5)<<p[i][j]<<"\t";
        }
        cout<<endl;
    }
}


int main(int argc,char* argv[])
{
    int a[3][4]= {0,1,2,3,4,5,6,7,8,9,10,11};
    fun(a);
    cout<<endl;
    fun1(a);
    cout<<endl;
    for(int i=0; i<3; ++i)
    {
        fun2(*(a+i));
    }
//      fun3(a);
//编译时发生错误。int [][]不能转换为int**
//error: cannot convert ‘int (*)[4]’ to ‘int**’ for argument ‘1’ to ‘void fun3(int**)’
////////////////////////////////////////
    int row=3, col=4;
/////////create the 2d dynamic array////
////////////////////////////////////////
    int **p=new int *[row];
    for(int i=0; i<row; ++i)
    {
//用p[i]指向第一个含有col个元素的数组///
        p[i]=new int[col];
//p[0]、p[1]、p[2]分别指向一个含有col个元素的数组,
    }
////////////////////////////////////////

    for(int i=0; i<row; ++i)
    {
        for(int j=0; j<col; ++j)
        {
            p[i][j]=i*j+j+99;
        }
    }

    cout<<"p="<<p<<"  sizeof(p)="<<sizeof(p)<<" *p = "<<*p<<endl;
    cout<<"p[0]="<<p[0]<<"\t"<<"*(p+0) = "<<*(p+0)<<endl;
    cout<<"p[1]="<<p[1]<<"\t"<<"*(p+1) = "<<*(p+1)<<endl;
    cout<<"p[2]="<<p[2]<<"\t"<<"*(p+2) = "<<*(p+2)<<endl;
    int b[2][5]= {0};
    cout<<"b="<<b<<"\t"<<"b[0]="<<b[0]<<endl;

    fun3(p);

////////////////////////////////////////
//////////删除自己申请的空间////////////
    for(int i=0; i<row; ++i)
    {
        delete []p[i];
        cout<<"delete p["<<i<<"]"<<"\t";
    }
    cout<<endl;
    delete []p;
    cout<<"delete p"<<endl;
////////////////////////////////////////
    cin.get();
//    return 0;

}
//
//[root@yutong array]# g++ -o twodimarraypara twodimarraypara.cpp
//[root@yutong array]# ./twodimarraypara 
//a[0][0]=  0   a[0][1]=  1     a[0][2]=  2     a[0][3]=  3     
//a[1][0]=  4   a[1][1]=  5     a[1][2]=  6     a[1][3]=  7     
//a[2][0]=  8   a[2][1]=  9     a[2][2]= 10     a[2][3]= 11     
//
//p[0][0]=  0   p[0][1]=  1     p[0][2]=  2     p[0][3]=  3     
//p[1][0]=  4   p[1][1]=  5     p[1][2]=  6     p[1][3]=  7     
//p[2][0]=  8   p[2][1]=  9     p[2][2]= 10     p[2][3]= 11     
//
//p[0]=  0      p[1]=  1        p[2]=  2        p[3]=  3        
//p[0]=  4      p[1]=  5        p[2]=  6        p[3]=  7        
//p[0]=  8      p[1]=  9        p[2]= 10        p[3]= 11        
//p=0x1adf010  sizeof(p)=8 *p = 0x1adf030
//p[0]=0x1adf030        *(p+0) = 0x1adf030
//p[1]=0x1adf050        *(p+1) = 0x1adf050
//p[2]=0x1adf070        *(p+2) = 0x1adf070
//b=0x7fff0c6e79f0      b[0]=0x7fff0c6e79f0
//p[0][0] =    99       p[0][1] =   100 p[0][2] =   101 p[0][3] =   102 
//p[1][0] =    99       p[1][1] =   101 p[1][2] =   103 p[1][3] =   105 
//p[2][0] =    99       p[2][1] =   102 p[2][2] =   105 p[2][3] =   108 
//delete p[0]   delete p[1]     delete p[2]     
//delete p
//

看完上述内容,你们对二维数组作为函数的实参以及定义函数形参的方法是什么有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. php中函数的实参和形参
  2. ​c语言中实参和形参的区别是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

二维数组 函数

上一篇:如何理解反射调用成员

下一篇:如何使用反射调用方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》