0

I am trying to make a craps game for school and my teacher/peers for some reason do not like using pointers.

I have the following code:

double get_wager_amount (void) {

    double wager;

    printf("How much would you like to wager?\n");
    scanf(" %f", &wager);

    return wager;
}

and would like to return the wager variable into another function that validates the wager amount based off of the amount in the user's bank account.

something like the code below but this obviously does not work because it re-prints everything that was in the wager and bank account function above.

int check_wager_amount (int *wager, int *account_balance)) {
    double wager = 0, balance= 0;

    if (wager <= get_wager_amount())
    {
        return 1;
    } if (balance <= get_bank_balance())
    {
        return 0;
    } else
    {
        printf("You either have no money or the program is not functioning correctly");
    }
}

can someone please explain how to pass return values of functions to other functions via pointers or references? I cannot find any information on how to do this in C. Thank you.

Dan
  • 1,466
  • 1
  • 13
  • 27
ntdog22
  • 279
  • 1
  • 5
  • 16
  • 2
    Why do you want to pass a pointer? – juanchopanza Feb 23 '15 at 22:43
  • 1
    The scanf specifier for `double` is `%lf` – M.M Feb 23 '15 at 22:43
  • 2
    You redeclare `wager` as a `double` immediately after using it as a function parameter as `int *wager`, and you never use `account_balance`. – wolfPack88 Feb 23 '15 at 22:44
  • Pointers are a bit overkill here. You're dealing with doubles and ints here, i.e. a few bytes in memory that could even fit into registers. I think you're breaking a fly on the wheel with pointers. – jan.h Feb 23 '15 at 22:48
  • This is just a guess, but have you covered variable scope yet? Your teacher is probably trying to help you not access locals out of scope (since that's a Bad Thing: see [this Q&A](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) ) – etheranger Feb 23 '15 at 22:52

1 Answers1

0

Do you want this?

check_wager_amount(&get_wager_amount()); // This should work, I guess

I am not sure if this is incorrect to use, but in case the above produces a error (pretty new to C myself), you could always just use

double wager = get_wager_amount(); // Store the variable somewhere
check_wager_amount(&wager); // Reference the variable

I also noticed that your code is invalid. You ask for a wager as a int, but later declare it as a double. A pointer is a int, but you should use a double* (not a int, and defenitly not a int*) to store your pointer.

yyny
  • 1,623
  • 18
  • 20
  • 4
    No, your first line of code will not work. The value returned by a function is not an object and you can't take its address. In fact `&get_wager_amount()` is legal; it's just equivalent to `get_wager_amount()` (due to some obscure semantics involving function addresses). – Keith Thompson Feb 23 '15 at 22:51
  • i am still confused. I am trying to get the return value of wager into the function check wager without having to set the function equal to a variable and calling it that way. Is there no way in C to pass or reference pointers from function to function or inherit variables from function to function? – ntdog22 Feb 23 '15 at 23:51
  • @ntdog22 Then you need to declare the function as `void get_wager_amount(double &wager)`. Have a look at [this](http://pages.cs.wisc.edu/~cs368-2/CppTutorial/NOTES/PARAMS.html) – Dan Feb 24 '15 at 00:26
  • @Dan Thank you for that. Appreciate the help. – ntdog22 Feb 24 '15 at 01:08
  • @Dan I didn't even know you could use & for a function parameter, I have always used * before. Thanks. – yyny Feb 25 '15 at 09:05