Jump to content
  • Advertisement
Sign in to follow this  
Afr0m@n

Pointers to struct members?

This topic is 4861 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 guys, sorry for posting a new topic so soon after a previous one, but this is something I need to know and I've no idea how to do it. I'm in the process of creating a enemy spawning system for my game, and I seriously need to know how to access member data of a struct that's pointed to by a void pointer. This is what I'm doing:
		//This shit right here enables us to generate enemies every half second.
		if(SDL_GetTicks() > enemy_timer + 500)
		{
			enemy_timer = SDL_GetTicks();
			ptr ptr_p = &e; //Let p point to enemy struct.
			listNode *enemy;

			//This adds a copy of the enemy struct to the global enemy list and returns a new node 
			enemy = addLast(EnemyList, ptr_p);
		}
		
Since I'm creating new copies of ptr_p (ptr is in this case, a typedef for a void pointer) each 500th millisecond in my mainloop, I need to access the data members of the struct before a copy of it is added to the linked list.. right (so I can change them for each new enemy)? I just don't know how to do this, though! :(

Share this post


Link to post
Share on other sites
Advertisement
If I understand you correctly, you have a "void * ptr", which points to a structure of type "Enemy", and you want to access the fields of the Enemy structure?

If so, you need to cast the pointer to a typed (rather than void) pointer first, eg:


Enemy e1;
void * ptr = &e1;

e1.name = "Fred";
((Enemy *)ptr)->age = 12;

Share this post


Link to post
Share on other sites
if p is a void * which points to the address of the struct e, simply dereference and access. But first, you will probably have to cast -- so I would recommend using a struct pointer instead of void. So if your struct is called "Enemy," something like this:


//This shit right here enables us to generate enemies every half second.
if(SDL_GetTicks() > enemy_timer + 500)
{
enemy_timer = SDL_GetTicks();
Enemy *ptr_p = &e; //Let p point to enemy struct.

(*ptr_p).somevalueinstruct = ...;
ptr_p->somevaludinstruct = ...;

listNode *enemy;

//This adds a copy of the enemy struct to the global enemy list and returns a new node
enemy = addLast(EnemyList, ptr_p);
}




As you can see, I added two ways to dereference and access. If you want to stick to void *, you will need to cast so that the bits are interpreted correctly.

Edit: Bah, beaten.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!