totally a newbie in C. I'm trying to put a 2D array into an existing memory space created by malloc. Here is the code:
int main()
{
    int **a;  //double pointer
    int i;
    void *ptr = malloc(sizeof(int)*4);  //2x2 array
    a = (int **)ptr;  //start of the array
    for(i=0; i<2; i++)  
            a[i] = (int *)a + i*2;
    printf("a: %p\n", a);
    printf("a[0]: %p\n", a[0]);
    printf("a[1]: %p\n", a[1]);
}
output:
a: 0x1976010
a[0]: 0x1976010
a[1]: 0x1976018
but when I try to access the element:
for(i=0; i<2; i++)
    for(j=0; j<2; j++)
        a[i][j] = j + i*10;
I got segment fault. Where did I do wrong? Thanks in advance!
 
     
     
     
     
     
    