I know two dimensional array is stored in memory as 1 dimensional array. So following the same logic I am trying to pass the array by reference using a single pointer as is done for 1 dimensional array. Below is my code:
#include<stdio.h>
void display(int *s)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%d ",s[i][j]);
}
printf("\n");
}
}
int main()
{
int s[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
printf("address of the array is %p\n",s);
printf("value is %p\n",*s);
int i;
printf("address of the repective array is\n");
for(i=0;i<3;i++)
{
printf("address of the array is %p\n",s[i]);
}
display(s);
return 0;
}
When i try to compile this get following message:
twodarray.c: In function ‘main’:
twodarray.c:25:2: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
display(s);
^
twodarray.c:2:6: note: expected ‘int **’ but argument is of type ‘int (*)[4]’
void display(int *s[3])
^
When i run the above code i get segmentation fault error.