1

So I'm new to coding, and trying to make a program in C, where I enter a float value and it returns the minimum number of coins needed to represent that value. But I keep getting this error message:

c:16:19: error: cannot take the address of an rvalue of type 'float

printf ("%f", &change(f));

#include <stdio.h>
#include <cs50.h>

float change(float n);

int main(void) {
    float f;
    do {
        f = get_float();
    } while (f < 0);

    change(f);
    printf("%f", &change(f));
}

float change(float n) {
    float pennies = 0.01;
    float nickles = 0.05;
    float dimes = 0.1;
    float quarters = 0.25;
    float coins = 0;

    do {
        n = n - quarters;
        coins++;
    } while (n >= quarters);

    do {
        n = n - dimes;
        coins++;
    } while (n >= dimes);

    do {
        n = n - nickles;
        coins++;
    } while (n >= nickles);

    do {
        n = n - pennies;
        coins++;
    } while (n != 0);

    return coins;
}
  • 2
    Why do you even *want* to pass the pointer in in that example? Your `printf` function isn't expecting a pointer there. – Steve Oct 27 '17 at 12:44
  • 1
    I'm voting to close this as being caused by a simple typographic error, or just nowhere near enough basic research on the language, neither of which create a useful thread for future readers. – underscore_d Oct 27 '17 at 12:46
  • 1
    `printf ("%f", &change(f));` tries to print the address of the value returned by `change()` as a float (since its an rvalue, you can't). You just want to print the value of the result of the function: `printf ("%f", change(f));` This makes the previous call to `change(f);` unnecessary. – 001 Oct 27 '17 at 12:46
  • Welcome to the site! Check out the [tour](https://stackoverflow.com/tour) and the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) for more about asking questions that will attract quality answers. You can also [edit your question](https://stackoverflow.com/posts/46975298/edit) to include more information if commenters ask. I agree with @JohnnyMopp. – cxw Oct 27 '17 at 12:48
  • Aside: please read [Why not use Double or Float to represent currency?](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) Although that was a java question, the accepted answer says *This issue isn't just for Java, it's for any programming language that uses native floating-point types*. – Weather Vane Oct 27 '17 at 12:51

2 Answers2

1

I am rusty on C, but here what I think is happening. There are lvalues and r values.

int a = 5;

In this line a is lvalue and 5 is rvalue. You can only take address of lvalue and not rvalue. When you call change(f) you get an rvalue for yourself, which you can then assign to lvalue like

float ff = change(f);

Now you can take address of ff. So either try to do as was suggested by @mco (if you want to print an actual value), or if you really want address do something like this

float ff = change(f);
printf("%i", &ff);

I changed %f to %i because printing address as float makes little sense.

sasha199568
  • 1,143
  • 1
  • 11
  • 33
  • 2
    `%i` is no better for printing pointers, as it may overflow or otherwise invoke UB. That should be `%p`. But it's clear that printing the address of the `float` is not what the OP wants. – underscore_d Oct 27 '17 at 13:18
0

Format %f takes floating point number argument, so you can pass the number returned by change(f) to printf as second argument:

printf ("%f", change(f));

Temporary results of expressions, like change(f), are rvalues and they do not have a memory location that can be accessed by &

mco
  • 83
  • 7