Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointers and Structures

Started by dinner_at_seven May 19, 2003 at 11:13 PM 8 replies 600+ views
Original Post
dinner_at_seven
dinner_at_seven
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.
Alpha_ProgDes
Alpha_ProgDes

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]
Beginner in Game Development?  Read here. And read here.  
kdogg
kdogg
I'd just pass a pointer to the entire struct [EDIT: the ENEMY struct, that is]. That would be the simplest way and there would be no speed loss.

[edited by - kdogg on May 20, 2003 12:46:30 PM]
JohnBolton
JohnBolton
MakeParticles(&enemy[0].particles)
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Sander
Sander
You could also make the MakeParticles() function a part of the Particle class and call

Emeny[0].particles.MakeParticles();

I think that''s the most elegant solution, but It depends on what your MakeParticles() function does.
Alpha_ProgDes
Alpha_ProgDes
kdogg:
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]
Beginner in Game Development?  Read here. And read here.  
JohnBolton
JohnBolton
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]
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
LordDeath
LordDeath
Jambolo is correct.
kdogg
kdogg
my bad.
dinner_at_seven
dinner_at_seven
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.