🔒 Locked
Pointers and Structures
Advertisement
Original Post
Hello all,
I was just wondering how I could go about solving a small problem I am having with pointers and structures.
I have this in a .h file of mine:
typedef struct
{
int xpos
int ypos
Particles particles
}
ENEMY;
ENEMY enemy[10];
(particles being a structure that i have made up)
so when i am trying to reference this from a function in my c file - such as this, and want to make a nother call to a function from within in this function i need a pointer to the particles within the strcuture
normally i would just go like this
MakeParticles(&particles)
but when i try to go like this:
MakeParticles(enemy[0].particles)
this does not work.
So i was wondering how i can go about getting a pointer to the structure.
Thanks in advance.
typedef struct
{
int xpos
int ypos
Particles particles
}
ENEMY;
ENEMY enemy[10];
void makeparticles (Particles*); //expecting a struct Particles
int main ()
{
makeparticles (& enemy[0].particles);
//... other stuff
}
void makeparticles (Particles *initParticles)
{
*(initparticles).x;
//or
initparticles->x;
// blah blah
}
mostly you'll have to post some code so we can see what the mistake is. but that's how you do it... afaik.
[edited by - Alpha_ProgDes on May 19, 2003 12:29:26 AM]
edit: Jambolo caught my oversight. thank you.
[edited by - Alpha_ProgDes on May 20, 2003 2:57:42 AM]
MakeParticles(&enemy[0].particles)
kdogg:
do you suggest this?
i too thought of the passing ENEMY* but then figured he wouldn't want to rewrite his function (whether a total or near-total rewrite, i'm unsure).
smarechal: from his code i figure he's using only C, not C++.
so i believe we should give him the most elegant C solution
edit: code tags looked funny.
[edited by - Alpha_ProgDes on May 20, 2003 3:26:05 AM]
[edited by - Alpha_ProgDes on May 20, 2003 3:27:01 AM]
do you suggest this?
makeParticles (&(enemy[0]).particles);
//i'm guessing it has the same properties(?) as this:
makeParticles (*(enemy[0]).particles);
i too thought of the passing ENEMY* but then figured he wouldn't want to rewrite his function (whether a total or near-total rewrite, i'm unsure).
smarechal: from his code i figure he's using only C, not C++.
so i believe we should give him the most elegant C solution
edit: code tags looked funny.
[edited by - Alpha_ProgDes on May 20, 2003 3:26:05 AM]
[edited by - Alpha_ProgDes on May 20, 2003 3:27:01 AM]
quote:
Original post by kdogg
That will look for the particles member of the address of the struct enemy[0], i.e., it won't compile.
kdogg, you are wrong. According to the C precedence rules &x[ n ].y is the same as &( ( x[ n ] ).y )
quote:
...
MakeParticles(enemy[0].particles)
this does not work. So i was wondering how i can go about getting a pointer to the structure.
The answer is:
MakeParticles( & enemy[0].particles )
[edited by - Jambolo on May 20, 2003 4:16:44 AM]
Thanks for all the help guys - had to go home and check my code and yes
the answer was
MakeParticles(&enemy[0].particles) just like it should have been - at one
stage i did have that and it wasnt working cause of another reason. Thanks
again for the quick response.
MakeParticles(&enemy[0].particles) just like it should have been - at one
stage i did have that and it wasnt working cause of another reason. Thanks
again for the quick response.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement