I am trying to make the switch to Emacs from VIM but I am having trouble getting the syntax checkers to work for a small C project. I have tried both Flymake and Flycheck as syntax checkers and both show a non-existent compiler error. I currently have 3 files, poker.c
cards.c
and cards.h
.
here is poker.c
#include "cards.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct Deck *deck = malloc(sizeof(struct Deck));
struct Hand *hand = malloc(sizeof(struct Hand));
clear_deck(deck);
deck = init_deck(deck);
deck = shuffle(deck);
int c = deal(deck, hand);
if(c != 5) {
printf("error: not enough cards delt");
} else {
print_hand(hand);
}
return 0;
}
and this is cards.h
typedef enum {
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
ACE,
} Rank;
typedef enum {
SPADES,
CLUBS,
HEARTS,
DIAMONDS,
} Suit;
struct Card {
Rank rank;
Suit suit;
int shuffled;
};
struct Deck {
struct Card cards[52];
int shuffled;
};
struct Hand{
struct Card cards[5];
};
void * shuffle(struct Deck *deck);
void * init_deck(struct Deck *d);
void clear_deck(struct Deck *d);
void print_deck(struct Deck *d);
void print_hand(struct Hand *h);
int deal(struct Deck *deck, struct Hand *hand);
Both syntax checkers show an error for the line
struct Hand *hand = malloc(sizeof(struct Hand));
as well as warnings for all the other functions. Here is the *Flycheck errors*
buffer
9 37 error invalid application of ‘sizeof’ to incomplete type ‘struct Hand’... (c/c++-gcc)
10 4 warning implicit declaration of function ‘clear_deck’... (c/c++-gcc)
11 3 warning implicit declaration of function ‘init_deck’... (c/c++-gcc)
11 8 warning assignment makes pointer from integer without a cast... (c/c++-gcc)
12 3 warning implicit declaration of function ‘shuffle’... (c/c++-gcc)
12 8 warning assignment makes pointer from integer without a cast... (c/c++-gcc)
13 3 warning implicit declaration of function ‘deal’... (c/c++-gcc)
17 5 warning implicit declaration of function ‘print_hand’... (c/c++-gcc)
I feel like that emacs isn't running the C pre processor before doing the syntax checking. Is there something I can do about this to make the syntax checker work properly?
amongst other things, the header file: cards.h, needs to have a 'include guard' suggest
#ifndef CARDS_H
#define CARDS_H
typedef enum {
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
ACE,
} Rank;
typedef enum {
SPADES,
CLUBS,
HEARTS,
DIAMONDS,
} Suit;
struct Card {
Rank rank;
Suit suit;
int shuffled;
};
struct Deck {
struct Card cards[52];
int shuffled;
};
struct Hand{
struct Card cards[5];
};
void * shuffle(struct Deck *deck);
void * init_deck(struct Deck *d);
void clear_deck(struct Deck *d);
void print_deck(struct Deck *d);
void print_hand(struct Hand *h);
int deal(struct Deck *deck, struct Hand *hand);
#endif // CARDS_H
I compiled the file: poker.c with additions for error checking.
With all warnings enabled (linux ubuntu 14.04 and gcc -Wall -Wextra -pedantic ...)
it cleanly compiled.
Here is the code I compiled.
#include "cards.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct Deck *deck = malloc(sizeof(struct Deck));
if( NULL == deck )
{ // then malloc failed
perror( "malloc for deck failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
struct Hand *hand = malloc(sizeof(struct Hand));
if( NULL == hand )
{ // then, malloc failed
perror("malloc for hand failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
clear_deck(deck);
deck = init_deck(deck);
deck = shuffle(deck);
int c = deal(deck, hand);
if(c != 5)
{
printf("error: not enough cards delt\n"); // \n so will immediate be output
}
else
{
print_hand(hand);
}
return 0;
} // end function: main
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.