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 ??
Asked
Active
Viewed 367 times
0

Munavar Fairooz
- 219
- 1
- 3
- 10
-
3In short: No, there isn't. – πάντα ῥεῖ Jul 19 '14 at 09:35
-
2possible duplicate of [passing 2D array to function](http://stackoverflow.com/questions/8767166/passing-2d-array-to-function) – πάντα ῥεῖ Jul 19 '14 at 09:36
-
Related: http://stackoverflow.com/questions/232691/how-can-i-get-the-size-of-an-array-from-a-pointer-in-c – alk Jul 19 '14 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 – Sathish Jul 19 '14 at 12:15
1 Answers
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);
}

A.s. Bhullar
- 2,680
- 2
- 26
- 32
-
It's probably worth noting that in C99, this also works with variable-length arrays. – The Paramagnetic Croissant Jul 19 '14 at 10:49