user2300867 user2300867 - 1 month ago 7
C Question

Unable to pass struct array to function in C

Learning C and need to populate array of five struct elements but I can't seem to figure out how to pass the struct array to a function and I keep getting error:

error: conflicting types for 'dealFiveHand'


Here is the code:

#define HAND_SIZE 5

void dealFiveHand(struct Card *wHand[]);

struct Card {
char suit;
char face;
};

int main(void)
{
struct Card *hand[HAND_SIZE];
dealFiveHand(hand);
}

void dealFiveHand(struct Card *wHand[])
{
...
}


Do I need to define and initialize a pointer and then pass that pointer to the function?

Answer

At least you need to move the definition of struct Card BEFORE function dealFiveHand prototype - that way the function knows about the type of its parameter.

struct Card {
    char suit;
    char face;
};

void dealFiveHand(struct Card *wHand[]);