Dynamically allocated struct array

Posted By: Fenriswolf

Dynamically allocated struct array - 04/01/08 09:43

Hello,

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] ...

The error reads:
 Quote:
subscript requires array or pointer type


Thanks
Posted By: Excessus

Re: Dynamically allocated struct array - 04/01/08 10:09

Maybe it has to do with operator precedence. Try "(thingy.array)[0]"
Posted By: zazang

Re: Dynamically allocated struct array - 04/01/08 10:18

i think u need to use thingy-> instead of thingy.
Posted By: Fenriswolf

Re: Dynamically allocated struct array - 04/01/08 11:47

Thank you both for replying.

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;

Posted By: Excessus

Re: Dynamically allocated struct array - 04/01/08 12:44

(thingy->array)[0] is a STUFF, not a STUFF*.

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[];"
Posted By: Fenriswolf

Re: Dynamically allocated struct array - 04/01/08 13:02

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!
Posted By: Excessus

Re: Dynamically allocated struct array - 04/01/08 13:05

Strange, those should be equivalent (** and * []), but whatever.. Glad I could help \:\)

BTW, you should change something else too:
thingy.array = (STUFF *)malloc(100 * sizeof(STUFF));

must be
thingy.array = (STUFF **)malloc(100 * sizeof(STUFF*));
now

And you'll have to malloc a STUFF for each index too:
(thingy.array)[n] = (STUFF*)malloc(sizeof(STUFF));
Posted By: Fenriswolf

Re: Dynamically allocated struct array - 04/01/08 13:48


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!
© 2024 lite-C Forums