初始化C中结构的静态数组

时间:2021-02-18 13:32:01

I'm implementing a card game in C. There are lots of types of cards and each has a bunch of information, including some actions that will need to be individually scripted associated with it.

我在c中实现了一个纸牌游戏,有很多种纸牌类型,每种都有很多信息,包括一些需要单独编写脚本的动作。

Given a struct like this (and I'm not certain I have the syntax right for the function pointer)

给定这样的结构体(我不确定我是否有函数指针的语法)

struct CARD {
    int value;
    int cost;
    // This is a pointer to a function that carries out actions unique
    // to this card
    int (*do_actions) (struct GAME_STATE *state, int choice1, int choice2);
};

I would like to initialize a static array of these, one for each card. I'm guessing this would look something like this

我想初始化一个静态数组,每个卡一个。我猜大概是这样的。

int do_card0(struct GAME_STATE *state, int choice1, int choice2)
{
    // Operate on state here
}

int do_card1(struct GAME_STATE *state, int choice1, int choice2)
{
    // Operate on state here
}

extern static struct cardDefinitions[] = {
    {0, 1, do_card0},
    {1, 3, do_card1}
};
  1. Will this work, and am I going about this the right way at all? I'm trying to avoid huge numbers of switch statements.

    这行得通吗?我这样做对吗?我试图避免大量的转换语句。

  2. Do I need to define the 'do_cardN' functions ahead of time, or is there some way to define them inline in the initialization of the struct (something like a lambda function in python)?

    我是否需要提前定义“do_cardN”函数,或者是否有某种方法在结构体的初始化中内联地定义它们(类似于python中的lambda函数)?

  3. I'll need read-only access to cardDefinitions from a different file - is 'extern static' correct for that?

    我需要从另一个文件对carddefinition进行只读访问——“extern static”对吗?

I know this is a lot of questions rolled into one but I'm really a bit vague about how to go about this.

我知道这里面包含了很多问题,但我真的有点不清楚该怎么做。

Thanks.

谢谢。

Edit:

编辑:

To be clear, my goal is to be able to do something like

很明显,我的目标是能够做一些类似的事情。

int cost = cardDefinitions[cardNumber].cost;

or

int result = cardDefinitions[cardNumber].do_action(state, choice1, choice2);

Instead of using huge switch statements all over the place.

而不是到处使用巨大的switch语句。

3 个解决方案

#1


34  

Your approach is exactly right.

你的方法完全正确。

  1. This will work, and is a good way to avoid huge switch statements.
  2. 这将会起作用,并且是避免大量切换语句的好方法。
  3. You can't define functions inline in C, they each must have a unique name.
  4. 不能在C中内联定义函数,每个函数都必须有一个惟一的名称。
  5. extern is what you want, not static. Change your body to be this:

    走着瞧,不是静态的。改变你的身体:

    struct CARD cardDefinitions[] = { 
        {0, 1, do_card0}, 
        {1, 3, do_card1} 
    }; 
    

    then in an appropriate header file:

    然后在适当的头文件中:

    extern struct CARD cardDefinitions[];
    

#2


3  

Your approach is right and will work. Your function pointer syntax is right, except that you don't use parameter names - just types:

你的方法是正确的,而且会奏效。您的函数指针语法是正确的,除了您不使用参数名称—只是类型:

int (*do_actions)(struct GAME_STATE *, int, int);

#3


1  

  1. That should work fine. It seems like you'd have a lot of functions if you're doing one per card, but maybe this particular game requires that level of control

    这工作好。如果你每一张牌都用一张牌的话,你会有很多的功能,但是也许这个游戏需要一定的控制水平

  2. You can't define them inline, but you can just do a forward declaration. You need to do &func_name in the struct initialization though

    您不能内联定义它们,但是您可以只做一个前向声明。不过,您需要在结构初始化中执行&func_name

  3. No; extern means a variable is declared in another file, so it doesn't make sense to have an extern variable that you're declaring at that location. Also, static means the variable is only accessible from the current file, which is the opposite of what you want. Making it read-only would require a getter function, but if you just want to make it accessible from another file declare it normally here (struct cardDefinitions[] = {...}) and in the other file use an extern (extern struct cardDefinitions[];)

    没有;extern意味着一个变量在另一个文件中被声明,所以你在那个位置声明的extern变量是没有意义的。而且,静态意味着只能从当前文件访问变量,这与您想要的相反。使它为只读将需要一个getter函数,但是如果您只是想让它从另一个文件访问,那么在这里声明它是正常的(struct carddefinition[] ={…}),而在另一个文件中使用extern (extern struct carddefinition [];)

#1


34  

Your approach is exactly right.

你的方法完全正确。

  1. This will work, and is a good way to avoid huge switch statements.
  2. 这将会起作用,并且是避免大量切换语句的好方法。
  3. You can't define functions inline in C, they each must have a unique name.
  4. 不能在C中内联定义函数,每个函数都必须有一个惟一的名称。
  5. extern is what you want, not static. Change your body to be this:

    走着瞧,不是静态的。改变你的身体:

    struct CARD cardDefinitions[] = { 
        {0, 1, do_card0}, 
        {1, 3, do_card1} 
    }; 
    

    then in an appropriate header file:

    然后在适当的头文件中:

    extern struct CARD cardDefinitions[];
    

#2


3  

Your approach is right and will work. Your function pointer syntax is right, except that you don't use parameter names - just types:

你的方法是正确的,而且会奏效。您的函数指针语法是正确的,除了您不使用参数名称—只是类型:

int (*do_actions)(struct GAME_STATE *, int, int);

#3


1  

  1. That should work fine. It seems like you'd have a lot of functions if you're doing one per card, but maybe this particular game requires that level of control

    这工作好。如果你每一张牌都用一张牌的话,你会有很多的功能,但是也许这个游戏需要一定的控制水平

  2. You can't define them inline, but you can just do a forward declaration. You need to do &func_name in the struct initialization though

    您不能内联定义它们,但是您可以只做一个前向声明。不过,您需要在结构初始化中执行&func_name

  3. No; extern means a variable is declared in another file, so it doesn't make sense to have an extern variable that you're declaring at that location. Also, static means the variable is only accessible from the current file, which is the opposite of what you want. Making it read-only would require a getter function, but if you just want to make it accessible from another file declare it normally here (struct cardDefinitions[] = {...}) and in the other file use an extern (extern struct cardDefinitions[];)

    没有;extern意味着一个变量在另一个文件中被声明,所以你在那个位置声明的extern变量是没有意义的。而且,静态意味着只能从当前文件访问变量,这与您想要的相反。使它为只读将需要一个getter函数,但是如果您只是想让它从另一个文件访问,那么在这里声明它是正常的(struct carddefinition[] ={…}),而在另一个文件中使用extern (extern struct carddefinition [];)