Jump to content
  • Advertisement

Archived

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

MARS_999

accessing pointers question

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

How do I assign a value to this pointer?
  
struct FOO
{
int a;
float *fp;
};

FOO *foo = new FOO[10];

foo[0].fp = 1.3f;
//this isn''t working? and neither does ->?


  
Why is it that I don''t have to use the & operator with a pointer type like foo[0].a in a fread() or fin.read() but when you say cout << foo[0].a it give the value and not the memory address? I am confused on this? Does fread() and fin.read() convert the arrays element 0 to the address but cout defaults to the value? I though you had to use -> with pointers and . with non pointers? when accessing class or struct memembers.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
fp = new float;
*fp = 1.3f;

i think the stream classes have overloads for everything or so

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
or in your case

foo[0].fp = new float;
*(foo[0].fp) = 1.3f;

not sure exactly, don''t have compiler

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hah, well that''s too bad, i just tried it, and it works. Well, hope you figure out how to do it.

Share this post


Link to post
Share on other sites
Why are you trying to assign a value to a pointer?

float *fp cannot be set to 1.3f..

This would then point to low memory. You could be experiencing
strange effects of rewritting code at runtime. {This would depend on what else your program is doing. The code sample you have shown would not.}

float fp could be set to 1.3f.

The quick solution is drop the asterisk in frount of the fp.

Armand.

Share this post


Link to post
Share on other sites
quote:
Original post by microdot
Try

foo[0].*pf = 1.3f;

though, APs should work as well.


Have tried that already and it don''t work either. Thanks

Share this post


Link to post
Share on other sites
quote:
Original post by Armand
Why are you trying to assign a value to a pointer?

float *fp cannot be set to 1.3f..

This would then point to low memory. You could be experiencing
strange effects of rewritting code at runtime. {This would depend on what else your program is doing. The code sample you have shown would not.}

float fp could be set to 1.3f.

The quick solution is drop the asterisk in frount of the fp.

Armand.


I am trying to assign a value to the variable.
float *fp = new float;
fp = 100.5f; //isn''t going work already now this
*fp = 100.5f; // now this is correct
I know that fp is a pointer and that *fp is a variable. My problem is when I have a struct or class and have a pointer in the struct or class and want to assign a value to it.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
or in your case

foo[0].fp = new float;
*(foo[0].fp) = 1.3f;

not sure exactly, don't have compiler




Ok I got this syntax to work finally. But why *(stuff) for a syntax.

  
*foo[0].fp = 1.5f;

THis works also.

I had to loop a new to each pointer in the struct. Problem solved. thanks

[edited by - Mars_999 on January 30, 2003 7:19:07 PM]

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!