Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize Static Array of Structs in C

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.

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?

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.

like image 863
russell_h Avatar asked Apr 14 '10 22:04

russell_h


People also ask

How do you initialize an array of structs?

If you have multiple fields in your struct (for example, an int age ), you can initialize all of them at once using the following: my_data data[] = { [3]. name = "Mike", [2]. age = 40, [1].

Can we create array of structure in C?

Hence, C enables us to declare an array of structures. We can evade declaring the different structure variables; instead, we can make an array containing all the structures that store the data of separate entities.

Can a struct be static in C?

Yes, thanks. The original wording could have been misinterpreted by beginning C programmers to develop a misconception along "Oh, static variables must be used with caution, so they must be bad in some way.

How are structs initialized in C?

Structure members can be initialized using curly braces '{}'.


1 Answers

Your approach is exactly right.

  1. This will work, and is a good way to avoid huge switch statements.
  2. You can't define functions inline in C, they each must have a unique name.
  3. 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[]; 
like image 195
Greg Hewgill Avatar answered Sep 19 '22 13:09

Greg Hewgill