0

Hey, I'm trying to understand how to float a pointer so that the pointer passes decimal
places, and i am lost in the sauce. is it supposed to look like this? Thanks in advance.

int object1;
   object1 = 100 ;
   int object2;
   object2 = 1000;
   float *ptr_object1 = &object1;
   float *ptr_object2= &object2;
4
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Prabakar
0

That code won't work because it is an error to cast an int pointer to a float pointer. The two objects have to be the same size, and int is not the same size as a float.

If you need to pass something with decimal places to a function then pass a pointer to a float.

void foo(float * num)
{

}

int main()
{
   float x = 0;
   foo( &x );
}
0

dragon's is a very nice and concise example of how to pass a float to a function.

here's how you could fix your code to work as it is.

int object1 = 100 ;
   int object2 = 1000;

   float object_f1 = (float)object1;
   float object_f2 = (float)object2;

   float *ptr_object1 = &object_f1;
   float *ptr_object2= &object_f2;

the concept here is called "casting", where you cast a variable of one type to another.

regardless, the pointer still has to be a type float.

i prefer dragon's method, its much more elegant. i'm just putting this out for illustration.

0

I agree with you there.

Its always joyful to play with pointers & casting pointers. So larryeis, learn a bit more about casting & pointers. I promise, it would be fun

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.