Jump to content
  • Advertisement

Archived

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

senkusha

pointer to struct inside of a struct doesn't work

This topic is 6941 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

I''m writing this larger program that doesn''t work very well. I''ve managed to isolate the problem into this smaller program. #include typedef struct { int hello; }Scene_t; typedef struct { int version; Scene_t *scene; }Ase_t; void main( void ) { Ase_t *ase = NULL; ase->scene->hello = 7; printf( "%d\n", ase->scene->hello ); return; } Now the problem happens when i assign 7 to hello. It just crashes. Anyone have any ideas thanksx

Share this post


Link to post
Share on other sites
Advertisement
You cannot assign 7 to hello, because hello simply isn''t a pointer to an int. You''re actually changing the address of the hello variable to 7, which is definitely not what you want to be doing. You need to declare the hello variable like this:

int * hello;

Or write the line that you''re assigning 7 to hello like this:

ase->scene.hello = 7;

That kinda looks bizarre, but you must do one of the above. Good luck!




ColdfireV

Share this post


Link to post
Share on other sites
quote:
Original post by senkusha

void main( void )
{
Ase_t *ase = NULL;
ase->scene->hello = 7;


Ok, you just declared ase to point to memory location 0. Chances are, there's nothing important there. Then you try modifying a struct member, which isn't gonna work cos there is no struct allocated there. In fact, you probably don't have the right to access that bit of memory, so Windows will probably bomb you out with an Illegal Operation, and show you that one of its registers is equal to 0, or near enough.

You need to allocate memory for a struct that you intend pointing to. This is where malloc() comes in - read the docs for this if you don't understand it, cos I'm a 'new' person myself and can't help you.

Hope that helps.

Edited by - Kylotan on 4/22/00 2:14:55 PM

Share this post


Link to post
Share on other sites
Just to extend what ColdfireV said.

I would reccemend the second of his two suggestions, since it would require less work. If you assigned "hello" 7 before giving it an int to point to using "ase->scene->hello" it would cause major problems. Next here is an explination on why the second one works...

ase->scene.hello is really (*ase).scene.hello. ase is a pointer, the "->" operator is short hand for "(*ptr)." By using "ase->scene->hello" which is what you thought it should be, you were really saying (*(*ase).scene).hello Which doesn''t work since scene isn''t a pointer. What this says is "indirectly use the pointer ase to get the pointer scene, indirectly use scene to get hello"

You probably didn''t need the full explination, but I put it in just incase some newbies read this post and were curious as to why.

-Omalacon

Share this post


Link to post
Share on other sites
Thanks for the help. Kylotans'' suggestion was what the doctor ordered. I acctually knew this, but for some reason it just escaped my mind.

thanks

Share this post


Link to post
Share on other sites
Yeah, Kylotan! To the rescue again! I just noticed that too, you have to allocate memory for a new ase_t. But what I said still stands, I believe. AFTER you create a new ase_t, then you must use one of the two lines I posted, or else you might run into the same problems. Or maybe you don''t, but who knows? Run it yourself first.


ColdfireV

Share this post


Link to post
Share on other sites

  • 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!