Search code examples
cdeclaration

Declaration in C Language


I tried to declaration this to C programming, but it turns out an error. Is there a mistake from this?

Error

poker_comp.c: In function ‘newdeck’:
poker_comp.c:40:15: warning: assignment to ‘Card *’ {aka ‘struct card *’} from incompatible pointer type ‘Card *’ {aka ‘struct card *’} [-Wincompatible-pointer-types]
   40 |     deck->top = newcard(p,s);
      |               ^
poker_comp.c:41:12: warning: assignment to ‘Card ’ {aka ‘struct card *’} from incompatible pointer type ‘Card *’ {aka ‘struct card **’} [-Wincompatible-pointer-types]
   41 |     cursor = deck->top;
      |            ^

Code

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;

Solution

  • typedef enum boolean {false, true} flipped;`
    

    is ending with a backquote, and that is wrong.

     struct card[52];
    

    is wrong (and is equivalent to Card[52];, which is also wrong); maybe you want Card card_deck[52]; and later use card_deck instead of card...

    If you use GCC (actually GCC 10 on Ubuntu 20) on your code aprilia.c as gcc -Wall -Wextra -g -c aprilia.c you get error messages:

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

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

    Refer also to some recent draft C standard, such as n1570 or something newer.

    Consider taking inspiration from existing free software coded in C, such as GNU Bash.

    Consider also using some static analysis tool, e.g., Frama-C or Clang analyzer (or, perhaps in mid-2021, Bismon).

    For tagged union types in C, consider having some union field inside some struct.