Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 258
kensing Avatar asked Mar 20 '26 11:03

kensing


1 Answers

#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));
}
like image 85
BLUEPIXY Avatar answered Mar 22 '26 00:03

BLUEPIXY