I have the below program in C. How did p[0][0] become 1? Can somebody help in understanding?
#include <stdio.h>
#include<stdlib.h>
main()
{
int i,j;
int **p=(int **)malloc(2 * sizeof(int *));
p[0]=(int*) malloc(2*sizeof(int));
p[1]=p[0];
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
p[i][j]=i+j;
printf("%d,%d,%d",i,j,p[i][j]);
printf("\n");
}
printf("%d,%d,%d",i,j,p[0][0]);
}
the output is as below
0,0,0
0,1,1
1,0,1
1,1,2
2,2,1
malloc()family is considered as a bad practice.p[1]isp[0], so when you made the assignmentp[1][0] = 1, that was equivalent top[0][0] = 1.main()In C, there are only two valid signatures formain()They are:int main( void )andint main( int argc, char *argv[] )