I want to display the elements of a 2D array by passing a pointer to a function. I successfully did it for 1D array.
#include<stdio.h>
void displaymat(int *a);
int main()
{
int a[3]={0,1,2};
int t[3][3]={1,2,3,4,5,6,7,8,9};
displaymat(a);
return 0;
}
void displaymat(int *a)
{
int i;
for(i=0;i<3;i++)
printf("%d\n",a[i]);/*works for single dimensional array*/
}
But when I use displaymat(t), it gives me an error saying incompatible pointer type.
However displaymat(&t[0][0]) seems to work. Why this apparent difference in passing pointer between 1D and 2D arrays?
int t[3][3]={1,2,3,4,5,6,7,8,9};is a 1-D array. The value of t[1][2] is dependent on whether data is stored in row-major or column-major order. Are you sure this is what you intended to do?