accessing pointers question

Started by
12 comments, last by MARS_999 21 years, 2 months ago
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.
Advertisement
fp = new float;
*fp = 1.3f;

i think the stream classes have overloads for everything or so
or in your case

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

not sure exactly, don''t have compiler
Nope that doesn''t work either. Thanks.
hah, well that''s too bad, i just tried it, and it works. Well, hope you figure out how to do it.
I've got to get some sleep...

[edited by - microdot on January 30, 2003 7:33:47 PM]
<span class="smallfont">That is not dead which can eternal lieAnd with strange aeons even death may die.   -- "The Nameless City" - H. P. Lovecraft</span>
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.
Armand -------------------------It is a good day to code.
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
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.
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]

This topic is closed to new replies.

Advertisement