#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3][3]={{0,1},{2,3},{5,6}},**b,i,j;
b=(int **)calloc(3,sizeof(int *));
for(i=0;i<3;i++)
{
b[i]=(int *)calloc(3,sizeof(int));
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=(i+1)*(j+1);
}
}
printf("%d %d %d %d\n",*(*a+4),**a+4,*(*b+4),**b+4);
printf("%d %d %d %d\n",*(a[2]-2),**(a+2),*(b[2]-2),**(b+2));
return 0;
}
Output I am getting :
3 4 2 5
3 5 6 3
According to me I should be getting:
3 4 4 5
3 5 4 3
Reason:
*(*b+4) = *(b[0]+4). b[0]+4 points to b[1][1] so this value should be 4, which is the value of matrix at 1,1 position
*(b[2]-2), Now b[2]-2 points to b[1][1], so value again should be 4
please tell me why am getting different output
b[0]+4points tob[1][1]? You allocated each row separately, they're not contiguous.aare contiguous because it's a 2-dimensional array.b[0]andb[1]to see that they differ by more than3.calloc()allocates contiguous memory, but it's not contiguous with other calls.