0

I cant seem to figure out how to set my printf statement to be able to print all elements of struct array that I'm passing to print function.

When I try to compile I get warnings:

format specifies type 'char *' but the argument has type 'char' [-Wformat]

I tried casting but still get an error.

When I run the program I just get:

Segmentation fault: 11

Here is the code:

struct Card {
    char suit;
    char face;
};
int main(void)
{
   ...
   struct Card *hand[HAND_SIZE];
   dealHand(deck, face, suit, hand); //deal the deck
   printHand(hand);
}

 void dealHand(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[], struct Card *wHand[])
{
    unsigned int c = 0;
    for (size_t card = 1; card <= HAND_SIZE; ++card) {
        for (size_t row = 0; row < SUITS; ++row) {
            for (size_t column = 0; column < FACES; ++column) {
                if (wDeck[row][column] == card){
                    struct Card aCard;
                    aCard.face = wFace[column];
                    aCard.suit = wSuit[row];
                    wHand[0] = &aCard;
                    c = c + 1;
                }
            }
        }
    }
}
void printHand(struct Card *wHand[]) {
    for (unsigned i = 0; i < HAND_SIZE; ++i) {
        printf("%c%c\n", wHand[i]->face, wHand[i]->suit);
    }
}

How can I print array of structs?

Edit: I replaced %s with %c but now getting warning:

incompatible pointer to integer conversion assigning to 'char' from 'const char *' [-Wint-conversion]
                    aCard.face = wFace[column];

New to pointers and not sure how to go about solving this one.

8
  • 1
    Your example doesn't have enough information for someone to help you. Try to shrink your example to something that compiles and is only ~20-30 lines long. (You might find the issue yourself this way!) Commented Nov 6, 2016 at 7:27
  • And error message is quite clear. You are passing wrong type of argument. Commented Nov 6, 2016 at 7:28
  • Try to parse the warning. It actually is trying to tell you something important, and this important thing is not "you can get rid of me with a cast". Commented Nov 6, 2016 at 7:31
  • provide the structure of the struct. try with %c Commented Nov 6, 2016 at 7:31
  • Do you really think that the problem is that you're trying to print all values of a struct in an array? Don't you think maybe the problem is simpler than that? Commented Nov 6, 2016 at 7:36

1 Answer 1

1

Since suit and face of type char. You must use %c in printf().

printf("%c%c\n", wHand[i]->face, wHand[i]->suit);
Sign up to request clarification or add additional context in comments.

3 Comments

Mi error seems to be in dealHand function. I now get warning: incompatible pointer to integer conversion assigning to 'char' from 'const char *' [-Wint-conversion] and it prints ?R instead of desired output
@user2300867 : And you have not show us dealHand() function. We can not simply make guess.
@user2300867 Then you should report that information in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.