我如何访问枚举中的变量

how can i access a variable in enum

#define NUMBER_OF_CARDS 54

typedef enum type{
        QUEEN;
        JACK;
        KING
} CardTypes;

typedef struct game{
        CardTypes cards[NUMBER_OF_CARDS];
        struct{
               int hearts;
               int spades;
               int clubs;
               int diamonds;
        }
        int players_cards;
}GameState;

我有类似的东西,我想在调用此函数时访问 enum 中的任何变量

void set_cards(GameState gamestate, int x, int y, CardTypes cardtypes){
         gamestate.cards[x * y] = cardtypes;
}

void generate_game(GameState gamestate){
         /*
                some code 
         */
         if(variable == 0){
                set_cards(gamestate, x, y, gamestate.cards[NUMBER_OF_CARDS].JACK; 
                //This is what I have tried but it doesn't work

希望你明白我的意思,因为我真的不知道如何更好地解释这个。

set_cards(gamestate, x, y, gamestate.cards[NUMBER_OF_CARDS].JACK;
//this is what I have tried but it doesn't work

请忽略代码中的任何不准确之处。对我来说重要的是如何访问函数 generate_game() 中的任何枚举变量。 就在这里:if(variable == 0){ set_cards(gamestate, x, y, gamestate.cards[NUMBER_OF_CARDS].JACK; //This is what I have tried but it doesn't work

你的类型没有太多意义。卡片由其颜色和类型定义。

typedef enum {
        QUEEN,
        JACK,
        KING,
        //you neeed some more
} CardTypes;

typedef enum {
    HEART,
    SPADE,
    CLUB,
    DIAMOND,
} CardColour;

typedef struct
{
    CardTypes type;
    CardColur colour;
}Card;

Card deck[54];

获取方式:

void foo(Card *card)
{
    Card card1;

    card1.colour = HEART;
    card1.type = JACK;

    card -> colour = DIAMOND;
    card -> type = KING;

    card[34].colour = CLUB;
    card[34].type = QUEEN;
}

根据@Aconcagua 编写的内容,您的代码应该使用指针:

// gamestate is a structure , so it must be passed as pointer to enable modification to be seen by caller
void set_cards(GameState *gamestate, int x, int y, CardTypes cardtypes){
     gamestate->cards[x * y] = cardtypes;
}

void generate_game(GameState *gamestate){ // here also pointer so caller know the changes 
     /*
            some code 
     */
     if(variable == 0){

        // next depends on what you intend to do :
        // 1- set the current games rate card with value of last card
        set_cards(gamestate, x, y, gamestate->cards[NUMBER_OF_CARDS-1]); 

        // 2- set the current gamestate to JACK
        set_cards(gamestate, x, y, JACK);