3

I'm trying to do the following, but the compiler is complaining about brackets, however, I can't find my way to an alternative.

struct cards {
  char faces[13][6], suits[4][9];
}

typedef struct cards cards;

void init_struct(cards *s) {
  s->suits = {"hearts","spades","clubs","diamonds"};
  s->faces = {"ace","two","three","four","five",
              "six","seven","eight","nine"
              "ten","jack","queen","king"};
}

I realize that there are several possible duplicate threads out there, but none of them has led me on the track. I hope one of you can :) Thanks

0

4 Answers 4

2
#include <string.h>

typedef struct cards {
    char faces[13][6], suits[4][9];
} cards;

cards base_card = {
    {"ace","two","three","four","five",
     "six","seven","eight","nine", //forgot "," at nine after
     "ten","jack","queen","king"},
    {"hearts","spades","clubs","diamonds"}
};

void init_struct(cards *s) {
    memcpy(s, &base_card,sizeof(cards));
}
Sign up to request clarification or add additional context in comments.

7 Comments

How can that work? You are copying a pointer-to-string into an array of characters!
@trojanfoe Would you kidding?
Would love to do it like this, however I'm having some errors: warning: initializer-string for array of chars is too long warning: (near initialization for ‘base_card.faces[8]’) - my code looks exactly like yours, except I have the 'cards base_card' declaration in my init_struct function (seems that it doesn't make any difference)
@kensing you forgot ','. #include <string.h> for memcpy
@kensing You need not even call memcpy, *s = base_card works too (structs are assignable).
|
0

The direct initialization syntax can only be used for initialization, not assignment. You cannot do this, for example:

char p[2][5];
p = {"a", "b"}; //error

That's why it fails to compile. Try strcpy-ing string by string

strcpy(s->suits[0], "hearts");
strcpy(s->suits[1], "spades");
...etc

or, alternatively, initialize a temporary array and then copy it

char suits_tmp[4][9] = {"hearts","spades","clubs","diamonds"};
memcpy(s->suits, suits_tmp, 4*9);

Comments

0
#include <string.h>
#include <stdio.h>

struct cards {
  const char** suits;
  const char** faces; 
};

typedef struct cards cards;

const char* suits[4] = {"hearts","spades","clubs","diamonds"};
const char* faces[13] = {"ace","two","three","four","five",
              "six","seven","eight","nine"
              "ten","jack","queen","king"};
int main()
{
    cards deck;
    deck.suits = suits;
    deck.faces = faces;
    printf(deck.suits[0]);
    return 0;
}

This works as well. Uses no pointers.

Clarification

I know mine is the quick and dirty answer, but there is no strcpy or memcpy or a long list of assignments. If your plan is to use the standard deck of cards for your game, then it would be a constant set of values anyway. If your intent is to have different types of decks, then my answer may not be adequate. Yes, it doesn't have a init_struct function, but you could easily modify it for your intent (since I am not well versed in C and malloc.)

Comments

0

Use const char * within your struct (I assume there is no requirement to modify the actual content of the suit/face value) and initialise them individually:

struct cards {
    const char *suits[4];
    const char *faces[13];
};

typedef struct cards cards;

void init_struct(cards *s)
{
    s->suits[0] = "hearts";
    s->suits[1] = "spades";
    s->suits[2] = "clubs";
    s->suits[3] = "diamonds";
    s->faces[0] = "ace";
    s->faces[1] = "two";
    s->faces[2] = "three";
    s->faces[3] = "four";
    s->faces[4] = "five";
    s->faces[5] = "six";
    s->faces[6] = "seven";
    s->faces[7] = "eight";
    s->faces[8] = "nine";
    s->faces[9] = "ten";
    s->faces[10] = "jack";
    s->faces[11] = "queen";
    s->faces[12] = "king";
}

Of course, if you just want a one-off set of cards, which is reasonable, then this will work:

struct
{
    const char *suits[4];
    const char *faces[13];
} cards =
{
    {"hearts","spades","clubs","diamonds"},
    {"ace","two","three","four","five",
        "six","seven","eight","nine",
        "ten","jack","queen","king"}
};

2 Comments

thanks, even though that's what I was afraid that I had to do :)
@remyabel you've downvoted both me and Armen Tsirunyan for what reason? Is it tactical downvoting?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.