0

Is there any way to pass 2D array to a function as function(arr,m,n) and the function defenition as void function(int **p,int m,int n) ie, Without explicitly specifying array size ??

4
  • 3
    In short: No, there isn't. Commented Jul 19, 2014 at 9:35
  • 2
    possible duplicate of passing 2D array to function Commented Jul 19, 2014 at 9:36
  • Related: stackoverflow.com/questions/232691/… Commented Jul 19, 2014 at 11:20
  • No. There is no way to pass the 2D array to a function Without explicitly specifying array size. for both 1D and 2d array you need to pass the array size to a function Commented Jul 19, 2014 at 12:15

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's probably worth noting that in C99, this also works with variable-length arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.