Initialize Static Array of Structs in C

后端 未结 3 1643
暗喜
暗喜 2021-02-03 20:34

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 assoc

3条回答
  •  长情又很酷
    2021-02-03 20:41

    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[];
      

提交回复
热议问题