C语言 在 C 中初始化静态结构数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2641473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:10:40  来源:igfitidea点击:

Initialize Static Array of Structs in C

carraysstaticstruct

提问by russell_h

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

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

  1. 这会起作用吗,我是否以正确的方式进行处理?我试图避免大量的 switch 语句。

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

  3. 我需要对不同文件中的 cardDefinitions 进行只读访问 - '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 语句。

回答by Greg Hewgill

Your approach is exactly right.

你的方法是完全正确的。

  1. This will work, and is a good way to avoid huge switchstatements.
  2. You can't define functions inline in C, they each must have a unique name.
  3. externis 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[];
    
  1. 这将起作用,并且是避免大量switch语句的好方法。
  2. 你不能在 C 中定义内联函数,它们每个都必须有一个唯一的名称。
  3. extern是你想要的,不是static。把你的身体改成这样:

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

    然后在适当的头文件中:

    extern struct CARD cardDefinitions[];
    

回答by caf

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

回答by Michael Mrozek

  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_namein the struct initialization though

  3. No; externmeans 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, staticmeans 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[];)

  1. 那应该可以正常工作。如果你每张卡做一个,似乎你会有很多功能,但也许这个特定的游戏需要那种程度的控制

  2. 你不能内联定义它们,但你可以做一个前向声明。你需要&func_name在结构初始化中做

  3. 不; extern意味着在另一个文件中声明了一个变量,因此在该位置声明一个外部变量是没有意义的。此外,static意味着该变量只能从当前文件访问,这与您想要的相反。让它只读需要一个 getter 函数,但如果你只是想让它从另一个文件访问它,请在此处正常声明它 ( struct cardDefinitions[] = {...}) 并在另一个文件中使用 extern ( extern struct cardDefinitions[];)