🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Void Pointers

Started by
3 comments, last by GameDev.net 24 years, 8 months ago
There's no problem with your code. I tried it just to make sure, and it works fine.
Advertisement
What compiler and what version are you using? Many compilers have minor iostream bugs, especially with reguards to pointers.
Also, if I might ask, why aren't you using a float* for the function in the first place (if the reason is tied to other areas of your program, it makes sense, but just wondering out of curiosity).
I'm not using float * for the function because I need to be able to pass anything to this function, not just arrays of floats.

I use Mingw32 (GNU Win32 compiler).

I've found that the problem is that the array of floats is going out of scope in my code, before the function which takes a void pointer is called. But I can't seem to fix this. myFunction in reality takes a structure, one of the properties of this structure is a (void *).

------------------
LoungePig
OpenGUI

I have an array of floats which I pass to a function which takes a void pointer like so:

float myArray[2];

myArray[0] = 1.8f;
myArray[1] = 0.9f;

myFunction( (void *) myArray );

Then in myFunction I cast these back to a float array, and write the values to stdout, but I end up with stupid numbers that arent what I said in the first place:

void myFunction(void *param)
{
float *myArray = (float *) param;
cout << myArray[0] << " " << myArray[1] << endl;
}

Why is this happening?

Thanks

Use malloc() or new to create your array so it wont go out of scope. Youll need to keep track of it somewhere as you have to free() or delete it though.

-Geoff

This topic is closed to new replies.

Advertisement