函数传递二维数组的方法

来源:互联网 发布:淘宝金钻买家是假的吗 编辑:程序博客网 时间:2024/04/29 19:52

函数传递二维数组的方法有三种:

方法一:需要规定二维的大小

#include<stdio.h>#include<iostream>using namespace std ;void print(int b[][10]){    for(int i=0 ;i<10 ;i++)    {        for(int j=0 ;j<10 ;j++)          cout<<b[i][j]<<" " ;          cout<<endl ;    }}int main(){    int  a[10][10] ;    for(int i=0 ;i<10 ;i++)      for(int j=0 ;j<10 ;j++)           a[i][j]=i*10+j ;    print(a) ;}
方法二:形参声明为指向数组的指针(注意括号)

#include<stdio.h>#include<iostream>using namespace std ;void print(int (*b)[10]){    for(int i=0 ;i<10 ;i++)    {        for(int j=0 ;j<10 ;j++)          cout<<b[i][j]<<" " ;          cout<<endl ;    }}int main(){    int  a[10][10] ;    for(int i=0 ;i<10 ;i++)      for(int j=0 ;j<10 ;j++)           a[i][j]=i*10+j ;    print(a) ;}
方法三:形参声明为指针的指针

#include<stdio.h>#include<iostream>using namespace std ;void print(int **b){    for(int i=0 ;i<10 ;i++)    {        for(int j=0 ;j<10 ;j++)          cout<<b[i][j]<<" " ;          cout<<endl ;    }}int main(){    int  a[10][10] ;    for(int i=0 ;i<10 ;i++)      for(int j=0 ;j<10 ;j++)           a[i][j]=i*10+j ;    int *p[10] ;    for(int i=0 ;i<10 ;i++)       p[i]=a[i] ;       print(p) ;}



0 0
原创粉丝点击