简体   繁体   中英

passing 2d array to function with in c

有没有办法将2D数组作为function(arr,m,n)传递给函数,将函数定义作为void function(int **p,int m,int n)传递给函数,即,没有显式指定数组大小?

Let us C has a good explanation about how to pass two D array as parameter.

I usually use these two ways for passing 2D array as parameter. But you need to specify the size explicitly.

void display1(int q[][4],int row,int col){
    int i,j;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
            printf("%d",q[i][j]);
        printf("\n");
    }
}

void display2(int (*q)[4],int row,int col){
    int i,j;
    int *p;
    for(i=0;i<row;i++)
    {
        p=q+i;
        for(j=0;j<col;j++)
            printf("%d",*(p+j));
        printf("\n");
    }
}
int main()
{
   int a[3][4]={1,2,3,4,5,6,7,8,9,0,1,6};
   display1(a,3,4);
   display2(a,3,4);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM