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 ??
-
3In short: No, there isn't.πάντα ῥεῖ– πάντα ῥεῖ2014-07-19 09:35:47 +00:00Commented Jul 19, 2014 at 9:35
-
2possible duplicate of passing 2D array to functionπάντα ῥεῖ– πάντα ῥεῖ2014-07-19 09:36:39 +00:00Commented Jul 19, 2014 at 9:36
-
Related: stackoverflow.com/questions/232691/…alk– alk2014-07-19 11:20:31 +00:00Commented 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 functionSathish– Sathish2014-07-19 12:15:48 +00:00Commented Jul 19, 2014 at 12:15
Add a comment
|
1 Answer
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);
}
1 Comment
The Paramagnetic Croissant
It's probably worth noting that in C99, this also works with variable-length arrays.