-4

I am having problem with the bolded variables. CLion said that those parameters are never accessed.

When I call the function open_turn, the turn_face, and turn_suit are said that they have not been initialized. But I do not want to initialize those variables by assigning values to them since the values are only determined after the function is called.

How do I pass int turn_card, int turn_f, and int turn_s into the function open_turn? Then assigning value of int turn_card to int turn, int turn_f to int turn_face, and int turn_s to turn_suit?

P/s: At this moment, parameters int turn_f and int turn_s are said to be declared but never accessed.

void open_turn(int current_deck[], int turn_card, int turn_f, int turn_s);

int main() {
    int turn;
    int turn_face;
    int turn_suit;

open_turn(deck, turn, turn_face, turn_suit);

}

void open_turn(int current_deck[], int turn_card, int turn_f, int turn_s) {
    turn_card = current_deck[card_idx++];
    turn_f = turn_card%13;
    turn_s = turn_card/13;
Quan Bui
  • 175
  • 1
  • 13
  • Can you not relate to the compiler output? – Sourav Ghosh May 12 '16 at 15:42
  • Where do you ___use___ the `turn_s` and `turn_f` variables? – Sourav Ghosh May 12 '16 at 15:42
  • If you're just assigning turn_s and turn_f values in the function why do you need to pass them in at all? Or are you expecting them to be returned from the function in which case you'll need to pass in pointers to ints rather than ints. – OldBoyCoder May 12 '16 at 15:46
  • That warning means your code will compile, but doesn't do what you think it does. You need to tell us what you think that code will do. – Dour High Arch May 12 '16 at 15:48
  • I expect the variables turn_f and turn_s to assign their values to my turn_face and turn_suit variables in the main – Quan Bui May 12 '16 at 17:22
  • And that's not what the code you wrote does. Step through it in the debugger. @DevNull's answer does what you want. – Dour High Arch May 12 '16 at 17:37

2 Answers2

2

You're doing this all wrong. In , when you pass an argument to a function, you end up passing a copy of the the variable by value. Modifying the variable in the function has no (useful) impact, since you're just modifying a temporary copy of the variable which is discarded when the function call finishes. The compiler is right to barf out errors for this. To accomplish what you likely intend to do, you need to use pointers, and you still need to initialize them.

Note that you likely still have errors in your code since you haven't shown us how current_deck is defined.


Code Listing


/*******************************************************************************
 * Preprocessor directives.
 ******************************************************************************/
#include <stdio.h>


/*******************************************************************************
 * Function prototypes.
 ******************************************************************************/
void open_turn(int current_deck[], int turn_card, int turn_f, int turn_s);


/*******************************************************************************
 * Function definitions.
 ******************************************************************************/
int main(void)
{
    int turn;
    int turn_face;
    int turn_suit;
    open_turn(deck, &turn, &turn_face, &turn_suit);

    /* The following also works. */
    int* pTurn = &turn;
    int* pTurn_face = &turn_face;
    int* pTurn_suit = & turn_suit;
    open_turn(deck, pTurn, pTurn_face, pTurn_suit);

}

void open_turn(int current_deck[], int* turn_card, int* turn_f, int* turn_s)
{
    if ( !turn_card || !turn_f || !turn_s )
    {
        printf("Invalid input.\n");
        return;
    }

    *turn_card = current_deck[card_idx++];
    *turn_f = turn_card%13;
    *turn_s = turn_card/13;
}
Community
  • 1
  • 1
Cloud
  • 18,753
  • 15
  • 79
  • 153
  • I had no problem with the array current_deck[] because arrays are always treated as pointers when being passed to a function as a parameter, is that right? – Quan Bui May 12 '16 at 17:27
  • @JasonBui [Not really.](https://stackoverflow.com/questions/12676402/why-cant-i-treat-an-array-like-a-pointer-in-c). If the answer above resolved your issue, please consider marking it "accepted". – Cloud May 12 '16 at 17:38
  • Then how come I don't need any pointer for the array? – Quan Bui May 12 '16 at 17:48
  • An array decays to a pointer, so in the simple case, you can more or less treat them the same. If you get into the low level details, it becomes more complicated. – Cloud May 12 '16 at 18:06
  • I am still getting errors at **turn_card%13** and **turn_card/13**. It said that binary operator % and / are can't be applied to the expression of type int or int*. And the **turn_card = current_deck[card_idx++];** also has a warning: "Taking pointer from integer without the cast. Here is the output: **error: invalid operands to binary expression ('int *' and 'int') turn_s = turn_card/13;** – Quan Bui May 12 '16 at 18:12
  • @JasonBui Corrected a typo in my code (missing the indirection/dereferencing `*` unary operator). – Cloud May 12 '16 at 18:17
  • I'm still having the same problem with the **turn_card** variable. **error: invalid operands to binary expression ('int *' and 'int') turn_s = turn_card/13;** – Quan Bui May 13 '16 at 03:33
  • Got it fixed with `*turn_card = current_deck[card_idx++]; *turn_f = *turn_card % 13; *turn_s = *turn_card / 13;` – Quan Bui May 13 '16 at 04:15
0

If you want to return back a value from a function in C you must pass a pointer to the variable. For example:

#include <stdio.h>
void f( int x, int *p) {
  x = 0;   /* This only affects local param x */
  *p = 5;  /* copies x's value into variable pointed to by p */
}

int main() {
  int a = 1, b = 2;
  f(a, &b);  /* a passed by value, b's address is passed */
  printf("a = %d, b= %d\n", a, b); /* prints out a = 1, b = 5 */
}

Here the compiler might warn that the x variable in function f is assigned a value that is not accessed, since setting it to 0 had no affect on a in main. *p = 5 did not create a warning because it is affecting b's value which is then used in the printf call.

pcarter
  • 1,516
  • 14
  • 21