4
typedef enum
{
    HEARTS = 0, 
    SPADES, 
    DIAMONDS, 
    CLUBS
}Suits; //here HEARTS = 0, SPADES = 1, DIAMONDS = 2, and CLUBS = 3  

int main()
{
    Suits hand;
    play(hand);
    return 0;
}

void play(Suits hand)
{
    printf("Testing.\n");
}

When I compile something similar to this, the compiler gives me the error: implicit declaration of function 'play' and warning: 'menu' may be used uninitialized in this function. How can I fix these problems?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Bonnie
  • 461
  • 3
  • 11
  • 14

2 Answers2

4

When the compiler meets the call to play it doesn't know that function yet, since it's declared below. Thus the warning. Just move the declaration before main

typedef enum{HEARTS = 0, SPADES, DIAMONDS, CLUBS}Suits; //here HEARTS = 0, SPADES = 1, DIAMONDS = 2, and CLUBS = 3  

void play(Suits hand)
{
  printf("Testing.\n");
}

int main()
{
  Suits hand;
  play(hand);
  return 0;
}

To remove the 2nd warning, you declare

Suits hand;

but hand is not initialized yet, thus the compiler warns you that the value you provide to the function play is not initialized.

Just set a value for hand :

Suits hand = SPADES;
Déjà vu
  • 28,223
  • 6
  • 72
  • 100
0

As you've declared it above, Suits defines a type that can only contain one of HEARTS, SPADES, DIAMONDS, CLUBS. So when you declare hand as a Suit type, it can take values that you defined in your enum.

Try defining hand as a card type:

hand = SPADES;

then call play(hand).

Think of it similarly to declaring an int.

int x;

x is of an integer type and must contain values defined by this type. If you call a function

someFunc (x);

without intializing x, you'll get undefined behaviour.

EDIT: Also, put the definition of your play function above main (after the enum definition). Either that, or you need to declare your function above main:

void play(Suits hand);

Otherwise, main doesn't know what "play" refers to since the definition comes after it.

Chris
  • 556
  • 1
  • 4
  • 18