I’m currently trying to understand how to implement a 2-dimensional array of struct in C. My code is crashing all the time and I’m really about to let it end like all my approaches getting firm to C: garbage. This is what I got:
typedef struct {
int i;
} test;
test* t[20][20];
*t = (test*) malloc(sizeof(test) * 20 * 20);
My glorious error:
error: incompatible types when assigning to type ‘struct test *[20]’ from type ‘struct test *’
Do I have to allocate the memory seperately for every 2nd dimension? I’m getting nuts. It should be so simple. One day I will build a time-machine and magnetize some c-compiler-floppies…
This should be enough:
That will declare a 2-dimensional array of
testof size 20 x 20. There’s no need to use malloc.If you want to dynamically allocate your array you can do this: