简体   繁体   中英

C: initializing struct with an array of strings

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

#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));
}

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);

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"}
};
#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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM