Archived

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

accessing pointers question

This topic is 5431 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
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 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
First off, what your trying to do is dangerous. The initialization of the class objects creates the pointer variable, but the pointer variable itself doesn't store a valid memory address. It probably has some strange value, unless you set it explicitly in the constructor. If you try to assign a value to the memory the un-initialized pointer points to, then that's bad.

What you can do in your constructor is allocate memory for that pointer, and in the destructor release that memory. That way, you can use the pointer safely.

And the proper syntax is *(foo[0].fp) = 1.3f;. If that doesn't work, then something else might be wrong. Give us the compile/link error if that's what you are getting.

EDIT: Bah, you beat me

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

It makes the code look clearer.

quote:
THis works also.

Yeah, operator precedence makes it work out.



[edited by - Zipster on January 30, 2003 7:24:28 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by MARS_999
But why *(stuff) for ab syntax.
Duh. Obviously to make it clear what is being dereferenced.

edit: Bah, Zipster's edit beat me

[edited by - civguy on January 30, 2003 7:30:33 PM]

Share this post


Link to post
Share on other sites
So the procedure would be to allocate new memory with new on the foo[] and then loop through the foo[] and use new to allocate memory to the fp pointer. I think I got it!! And then when you delete the memory you need to delete the foo[].fp first with a loop and then delete the foo[] right? Thanks

Share this post


Link to post
Share on other sites