c - Why does emacs show these warnings and errors? -
i trying make switch emacs vim having trouble getting syntax checkers work small c project. have tried both flymake , flycheck syntax checkers , both show non-existent compiler error. have 3 files, poker.c
cards.c
, cards.h
.
here 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 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 error line
struct hand *hand = malloc(sizeof(struct hand));
as warnings other functions. here *flycheck errors*
buffer
9 37 error invalid application of ‘sizeof’ 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 integer without cast... (c/c++-gcc) 12 3 warning implicit declaration of function ‘shuffle’... (c/c++-gcc) 12 8 warning assignment makes pointer integer without 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 emacs isn't running c pre processor before doing syntax checking. there can make syntax checker work properly?
amongst other things, header file: cards.h, needs have '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 file: poker.c additions error checking.
with warnings enabled (linux ubuntu 14.04 , gcc -wall -wextra -pedantic ...)
it cleanly compiled.
here code compiled.
#include "cards.h" #include <stdio.h> #include <stdlib.h> int main() { struct deck *deck = malloc(sizeof(struct deck)); if( null == deck ) { // malloc failed perror( "malloc deck failed" ); exit( exit_failure ); } // implied else, malloc successful struct hand *hand = malloc(sizeof(struct hand)); if( null == hand ) { // then, malloc failed perror("malloc 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 immediate output } else { print_hand(hand); } return 0; } // end function: main
Comments
Post a Comment