I want to create a double pointer and make it point to an double dimension array, then output the content of the array through the pointer. However Gcc warn me that "assignment from incompatible pointer type". Just like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j;
int **p;
// First one. this doesn't work, why?
int a[2][3]={{1,2,3},{1,2,3}};
for(i=0;i<2;i++)
p[i]=a[i];
p=a;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
printf("p[%d][%d]=%d\n",i,j,a[i][j]);
// Second one. this one works.
int **b=(int **)malloc(2*sizeof(int *));
for(i=0;i<2;i++)
b[i]=(int *)malloc(3*sizeof(int));
for(i=0;i<2;i++)
for(j=0;j<3;j++)
b[i][j]=j+1;
p=b;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
printf("p[%d][%d]=%d\n",i,j,a[i][j]);
return 0;
}
Please could anyone tell me why the first one doesn't work while the second one does work?
Thanks to any suggestions.