0

I'm trying to return two values, u and v, which I pass as arguments to the function get_uv( float u, float v ).

Because double and float are not compatible with integer types, we can't convert them to pointer, and the following code give us error: cannot convert to a pointer type.

void calc_uv( float *u, float *v )
{
    u = (float *) 0.0f;
    v = (float *) 1.0f; 
}

void anyfunction()
{
    float u, v;
    calc_uv( &u, &v )

    printf("u: %f , v: %f", u, v);
}

It works if I use:

void calc_uv( float *u, float *v )
{
    *u = 0.0f;
    *v = 1.0f; 
}

But I don't understand why, as u and v were already pointers. Can you explain to me what's going on here?

What is the best way to modify and return two float from a function?

I saw I can use something like float *f = malloc(sizeof *f); but my goal is not to obtain a pointer by hook or by crook, just change the input values in the most elegant or concise way.

Leon
  • 554
  • 4
  • 18
  • You are trying to cast a scalar to a pointer, why do you think this should work? – anastaciu Jun 11 '20 at 10:11
  • You're trying to cast the floating-point value `0.0f` into a pointer. – interjay Jun 11 '20 at 10:11
  • The most elegant way is the way you already have at the second example. Why do you want to assign the pointers itself in the first example? – RobertS supports Monica Cellio Jun 11 '20 at 10:19
  • Note: all your functions are `void fnc(...)`, so they don't **return** anything. Your actual yestion should be something like "how to *change* the caller's variables from inside a function" – wildplasser Jun 11 '20 at 10:22
  • " we can convert them to pointer" What? No... `(float *) 0.0f` doesn't make any sense, because `0.0f` is not a pointer nor an address, it's a float constant. – Lundin Jun 11 '20 at 10:23

3 Answers3

3

But I don't understand why, as u and v were already pointers. Can you explain to me what's going on here?

Yes the function receives those variables as pointers. What do they contain? Address of the variables allocated in the anyfunction() function.

Now your intention should be to modify this "address" location by de-referencing the the pointer by doing *u which means "modify" the value at address, to a new value. But you were actually modifying the pointer address by virtue of doing u = (float *) 0.0f;, which is simply wrong, because pointers store addresses and not values.

Inian
  • 80,270
  • 14
  • 142
  • 161
2

"But I don't understand why, as u and v were already pointers. Can you explain to me what's going on here?"

The confusion you have seem to be based upon that you not correctly understand how pointers work in C and that we dereference pointers with the * operator to access the objects they actually point to.

If you use:

void calc_uv( float *u, float *v )
{
    *u = 0.0f;
    *v = 1.0f; 
}

*u in *u = 0.0f; and *v in *v = 1.0f; are not second declarations of the pointers itself.

These are operations on the pointers with the * operator to dereference the pointers u and v with the purpose to access the objects they point to in the caller.

Take a look at here:

What does "dereferencing" a pointer mean?

https://en.wikipedia.org/wiki/Dereference_operator


Side Note:

  • You say "I'm trying to return two values". When using pointers you do not actually return values.
1
float u, v;
calc_uv( &u, &v )

&u means reference to u (pointer)

&v means reference to v (pointer)

void calc_uv( float *u, float *v )

float *u - pointer to float

float *v - pointer to float

u = (float *) 1.0f;

you assign the pointer with the 1.0f converted to the float pointer. It does not make any sense in this case.

you need to dereference (access the object pointed by the pointer) pointer instead

*u = 1.0f 

I think that I understand your confusion. The * has different meanings depending where it is used.

  1. In declararion float *f; it shows the compiler that f is the pointer.
  2. In expressions *f = 1.0f; if shows the compiler that you dereference the pointer f . It does not declare/define it again.
0___________
  • 60,014
  • 4
  • 34
  • 74