pointer to struct inside of a struct doesn't work

Started by
4 comments, last by senkusha 24 years ago
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
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
[email=jperegrine@customcall.com]ColdfireV[/email]
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
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
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
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
[email=jperegrine@customcall.com]ColdfireV[/email]

This topic is closed to new replies.

Advertisement