Can you explain to me why does the first element ot the 2 dimentional array is 1 in this code?
#include <stdio.h> #include <stdlib.h> int main(void) { 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++){ printf("i=%d & j=%d\t",i,j); p[i][j] = i + j; printf("p[%d][%d]=%d\n",i,j,p[i][j]); printf("this is the result of the first element %d\n",p[0][0]); } printf("this is the result %d\n",p[0][0]); return 0; } The result is :
i=0 & j=0 p[0][0]=0
this is the result of the first element 0
i=0 & j=1 p[0][1]=1
this is the result of the first element 0
i=1 & j=0 p[1][0]=1
this is the result of the first element 1
i=1 & j=1 p[1][1]=2
this is the result of the first element 1
this is the result 1
Press to close this window...

p[0]andp[1]to refer to the same singular row?malloc().