Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 1,498 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
LucasJoshua, Baklazhan, Hanky27, firatv, wandaluciaia
19054 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Pointer to an array in a struct #255359
03/09/09 21:17
03/09/09 21:17
Joined: Mar 2009
Posts: 112
Germany
K
KDuke Offline OP
Member
KDuke  Offline OP
Member
K

Joined: Mar 2009
Posts: 112
Germany
[English]
Hello guys'n girls...

either I am dumb or I'm simply blind.
I'll simply explain the problem with some pseudo code.
The problematic line of code is marked as that in the sample code.
The compiler tells me "subscript needs array or pointer type" though, if my eyes don't lie to me, I declared aVariable as a pointer.

Is this unsupported in Lite-C free or a bug?
Maybe I'm just missing something?

I'm asking for your advice.

[German]
Hallo Jungs und Mädels...

entweder bin ich blöd oder blind.
Ich werde das Problem einfach mit etwas Pseudocode erklären.
Die problematische Programmzeile ist im Code markiert.
Der Compiler sagt mir nur "subscript needs array or pointer type", obwohl ich, wenn mich meine Augen nicht rügen, aVariable als Pointer deklariert habe.

Wird sowas von Lite-C free nicht unterstützt oder ist das ein Bug?
Vielleicht hab ich auch nur was übersehen?

Ich bitte um euern Rat.
Code:
typedef struct MYSTRUCT1
{
   somestuff;
   somestuff;
   somestuff;
} MYSTRUCT1;

typedef struct MYSTRUCT2
{
   MYSTRUCT1* aVariable;
   somestuff;
} MYSTRUCT2;

MYSTRUCT2 hereItGoes;

function MyCoolFunction()
{
   hereItGoes.aVariable = (MYSTRUCT1*)malloc(sizeof(MYSTRUCT)*iNeedItSeveralTimes);
   int index;
   for(index = 0; index <= iNeedItSeveralTimes; index++)
     hereItGoes.aVariable[index] = aValue;  //<-- problematic line of code is here
}



Using A7 Free
Click and join the 3dgs irc community!
Room: #3dgs
Re: Pointer to an array in a struct [Re: KDuke] #255383
03/10/09 02:14
03/10/09 02:14
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Here is my interpretation of your problem. I have problems with pointers myself,
but this way of doing what you want is what I always do. (barring typos)
This code is untested, but I have used this concept many times before.
Code:
MYSTRUCT2* hereItGoes;   //not sure if your declare of hereItGoes is valid

function MyCoolFunction()
{
   hereItGoes = (MYSTRUCT2*)malloc(sizeof(MYSTRUCT2));  //I always use malloc cause I can never be sure. 
   hereItGoes.aVariable = (MYSTRUCT1*)malloc((int)sizeof(MYSTRUCT)*iNeedItSeveralTimes);   //MAKE SURE the (int) is inside the malloc or it will fail
   //this line may also need to be "hereItGoes->aVariable = ..." but not sure
   int index;
   for(index = 0; index <= iNeedItSeveralTimes; index++)
     (hereItGoes->aVariable)[index] = aValue;  //you have to use the "->" in place of "." when dealing with strunct inside structs
     // and the PLACEMENT of the brackets around "(hereItGoes->aVariable)[index]" is VERY important
}



"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Pointer to an array in a struct [Re: EvilSOB] #255391
03/10/09 05:54
03/10/09 05:54
Joined: Mar 2009
Posts: 112
Germany
K
KDuke Offline OP
Member
KDuke  Offline OP
Member
K

Joined: Mar 2009
Posts: 112
Germany
Hi EvilSOB,

[English]
thank you very much for the answer.
It definitely has been the brackets around (hereItGoes.aVariable)[index] !
Wouldn't have thought Lite-C needs brackets at this place eek

[German]
vielen danke für die Antwort.
Es waren eindeutig die Klammern um (hereItGoes.aVariable) !
Dachte nicht, dass Lite-C Klammern an dieser Stelle braucht eek

greetings / mit freundlichen Grüßen
K-Duke


Using A7 Free
Click and join the 3dgs irc community!
Room: #3dgs
Re: Pointer to an array in a struct [Re: KDuke] #255392
03/10/09 06:21
03/10/09 06:21
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
It does, never quite figured out why though, explainations didnt make sense.
Too many pointers...


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Pointer to an array in a struct [Re: EvilSOB] #255441
03/10/09 14:53
03/10/09 14:53
Joined: Dec 2008
Posts: 271
Saturnus Offline
Member
Saturnus  Offline
Member

Joined: Dec 2008
Posts: 271
If I'm not mistaken, this is because square brackts have a higher precedence than dot operators (obj.member) and arrow operators (obj->member).

This means, if we had something like "obj->member[N]", the Nth index of member would be accessed, before getting the reference to that member ("obj->member"). To avoid this, we can use parentheses, which have an even higher precedence than square brackets. This way we can access the Nth index after getting the pointer to the member.

I hope this was somehow understandable even though my English is clumsy.

Re: Pointer to an array in a struct [Re: Saturnus] #255443
03/10/09 15:00
03/10/09 15:00
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Notclumsy at all, thanks for the clarity, I had always wondered and have
never before gotten an answer that I could understand clearly.
My thanks...


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Pointer to an array in a struct [Re: EvilSOB] #255481
03/10/09 18:49
03/10/09 18:49
Joined: Mar 2009
Posts: 112
Germany
K
KDuke Offline OP
Member
KDuke  Offline OP
Member
K

Joined: Mar 2009
Posts: 112
Germany
Thanks Kombucha for clarifying this!
Nice to know.

By the way I gotta agree with EvilSOB... your english is pretty good ;-)


Using A7 Free
Click and join the 3dgs irc community!
Room: #3dgs

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