View Full Version : C++ Type Casting

11-25-2008, 01:43 AM
I am having some trouble casting from float to float* in C++.

Here is an example of what I have (and yes, I need to use float*):

float *MA[4];
MA[0] = (float*)1.0F;

11-25-2008, 02:30 AM
You can't do that, the MA array is an array of pointers pointing to float variables. With that casting you are saying the address of the first variable that the pointer points to is 1.

To use pointers like that you would have to do one of the following:

float first = 1.0F;
float *MA[4];
MA[0] = &first;


float *MA[4];
MA[0] = new float;
*(MA[0]) = 1.0F;

With the last example of course you have to delete the pointer at some point before your program ends otherwise it will leak memory.

11-25-2008, 02:36 AM
Thanks, that fixed it. I have another question now. I am trying to multiply two float* types together, and I get the following compile error:

error: invalid operands of types ‘float*’ and ‘float*’ to binary ‘operator*’

any suggestions on that one?

11-25-2008, 05:17 AM
Yes, you need to de-reference the pointers before you multiply the numbers together. (You know with the *)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum