I have some problems using a dynamically allocated struct array that I hope someone could help me out with.
Code:
typedef struct THINGY
{
/* should point to an array of STUFF */
STUFF *array;
}THINGY;
[...]
THINGY *thingy = malloc(sizeof(THINGY));
/* allocates an array of length 100 */
thingy.array = (STUFF *)malloc(100 * sizeof(STUFF));
/* and this causes an compiler error:
"subscript requires array or pointer type" */
thingy.array[0] = NULL;
The following line causes an error while compiling: thingy.array[0] ...
Obviously my problem was partially solved by using "(thingy.array)[0]".
Now I can access individual structs without errors: (thingy->array)[0].whatever = ..;
However, the following code causes the error again ("subscript requires.."): STUFF *stuff = (thingy->array)[0];
EDIT: All right, everything is working fine now! In the THINGY struct I have replaced the pointer to STUFF with a pointer to pointer to STUFF: STUFF **array;
If I remember correctly, object assignment doesn't work properly in lite-c yet, so you can't just change STUFF* to STUFF. You'd have to use memcpy. Or change the code to make thingy have an array of POINTERS to STUFFs: "STUFF* array[];"
Oops, I have edited my post without seeing your reply, sorry. By using a pointer to pointer in the thingy struct the code works without any problems. However now I also tried 'STUFF* array[]'. But obviously lite-C doesn't like that and refuses to compile. Thank you all the same, Excessus!
Now it really works without any trouble. Stupid me has witten a debug code that only checks the first index of the allocated array: (thingy.array)[0].something = 3; This works indeed. However, when testing higher indices the engine crashes as expected. Thank you for pointing this out!