carrayscalloc

Calloc a Two-Dimensional Array


To make a two dimensional array, I'm currently using the following:

int * own;

own = (int *)calloc(mem_size, sizeof(int));

for (i=0;i<mem_size;i++){
    own[i] = (int *)calloc(3, sizeof(int));
}

However, every time I reference own[i][j], I get an error saying that the subscripted value is neither array nor pointer nor vector.


Solution

  • Use:

    int ** own; // int**, not int*
    own = calloc(mem_size, sizeof(int*)); //int*, not int
                                          // And remove the explicit cast.