Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (degenerate_762, Aku_Aku, 7th_zorro, Ayumi), 1,080 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Dynamically allocated struct array #199752
04/01/08 09:43
04/01/08 09:43
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

Joined: Jan 2007
Posts: 221
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

Re: Dynamically allocated struct array [Re: Fenriswolf] #199759
04/01/08 10:09
04/01/08 10:09
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
Maybe it has to do with operator precedence. Try "(thingy.array)[0]"

Re: Dynamically allocated struct array [Re: Excessus] #199761
04/01/08 10:18
04/01/08 10:18
Joined: Oct 2003
Posts: 702
Z
zazang Offline
User
zazang  Offline
User
Z

Joined: Oct 2003
Posts: 702
i think u need to use thingy-> instead of thingy.


I like good 'views' because they have no 'strings' attached..
Re: Dynamically allocated struct array [Re: zazang] #199794
04/01/08 11:47
04/01/08 11:47
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

Joined: Jan 2007
Posts: 221
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;


Last edited by Fenriswolf; 04/01/08 12:46.
Re: Dynamically allocated struct array [Re: Fenriswolf] #199809
04/01/08 12:44
04/01/08 12:44
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
(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[];"

Re: Dynamically allocated struct array [Re: Excessus] #199819
04/01/08 13:02
04/01/08 13:02
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

Joined: Jan 2007
Posts: 221
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!

Re: Dynamically allocated struct array [Re: Fenriswolf] #199820
04/01/08 13:05
04/01/08 13:05
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
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));

Last edited by Excessus; 04/01/08 13:08.
Re: Dynamically allocated struct array [Re: Excessus] #199838
04/01/08 13:48
04/01/08 13:48
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

Joined: Jan 2007
Posts: 221

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!


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1