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;

Recommended Answers

All 3 Replies

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 );
}

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.