C语言声明

Declaration in C Language

我试图向 C 编程声明它,但结果是错误。这有什么错误吗?

错误: poker_comp.c:在函数“newdeck”中: poker_comp.c:40:15: 警告:从不兼容的指针类型“Card *”{又名“struct card *”} [-Wincompatible-pointer-types] 分配给“Card *”{又名“struct card *”} 40 | deck->top = newcard(p,s); | ^ poker_comp.c:41:12: 警告:从不兼容的指针类型“Card *”{又名“struct card **”} [-Wincompatible-pointer-types] 分配给“Card”{又名“struct card *”} 41 | cursor = deck->top; | ^

typedef enum boolean {false, true} flipped;`
typedef struct card{
    int pips;
    char suit;
    bool flipped;
    int *nextCard;
}Card;

typedef struct deck{
    struct card[52];
    Card **cards;
}Deck;

typedef struct player{
    char name;
    int *c1, *c2;
    int acc;
}Player;
typedef enum boolean {false, true} flipped;`

以反引号结尾,这是错误的。

 struct card[52];

是错误的(相当于Card[52];,也是错误的);也许你想要 Card card_deck[52]; 然后使用 card_deck 而不是 card...

如果您在代码 aprilia.c 上使用 GCC (actually GCC 10 on Ubuntu 20) 作为 gcc -Wall -Wextra -g -c aprilia.c,您会收到错误消息:

 gcc -Wall -Wextra -c /tmp/aprilia.c -o /tmp/aprilia.o 
 aprilia.c:1:44: error: stray ‘`’ in program
     1 | typedef enum boolean {false, true} flipped;`
       |                                            ^
 aprilia.c:9:16: error: expected identifier or ‘(’ before ‘[’ token
     9 |     struct card[52];
       |                ^

参见 this C reference website. Read a good C programming book, such as Modern C

另请参阅一些最新的 C 标准草案,例如 n1570 或更新的内容。

考虑从 现有的 free software coded in C, such as GNU bash.

中汲取灵感

也考虑使用一些静态分析工具,例如Frama-C or Clang analyzer (or, perhaps in mid-2021, Bismon)

对于 C 中的 tagged union types,考虑在某些 struct.

中包含一些 union 字段