Archived

This topic is now archived and is closed to further replies.

A pointer to an array of structs

This topic is 5239 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, say I have a struct called Alien and I make an array of Aliens called GreenGuys. And say I want to make a pointer to say, GreenGuy[2]. I don''t know, maybe I want to pass this specfic GreenGuy to a battle function because he got picked at random as the target. Can I point directly to an array element? Would I even want to? Or are you supposed to make a pointer to the array and use pointer arithmetic to navigate to the element I want? I am just trying to work out the reasoning in my head. My guess is that I can''t just point to a specific element since I think the compiler only cares where the first element and uses pointer arithmetic itself to find the other elements and thats what I would have to do too, right?

Share this post


Link to post
Share on other sites
quote:
Original post by Anon Mike
No pointers to elements of arrays work fine.

Alien * p = &GreenGuy[2];

I''m not sure what it is that you''re confused about.



I was confused if that was valid and if people write their code like that or did it in a different or better way that I wasn''t thinking of.

For some reason I was thinking I would have to pass a pointer to the array to my function and then navigate to the element I wanted to access while in the function. Which I guess I could also do but its obviously faster to just do it directly. But I wanted to make sure I wasn''t over-looking anything since I am just finally getting a good grasp on pointers.

Share this post


Link to post
Share on other sites