Hello,
I am working on a project to create a game. This is a school assignment but for some reason I cannot figure out what's going on; perhaps I am missing something.
I need to create a deck of a struct called card and print the contents(for now) I compiled the program with gcc and no warnings so I assumed everything was good but when executed the out put file I got the error message
Segmentation fault (core dumped)
I was looking online and there are quite a few articles about this but I could not figure out what was going one.
Next I analysed the source file with "splint sample.c" and I got the following message:
sample.c: (in function main)
sample.c:37:11: Passed storage *myDeck contains 2 undefined fields: face, suit
Storage derivable from a parameter, return value or global is not defined.
Use /*@out@*/ to denote passed or returned storage which need not be defined.
(Use -compdef to inhibit warning)
sample.c:38:5: Parse Error. (For help on parse errors, see splint -help parseerrors.)
*** Cannot continue.
This is my source code. I have tried a lot: passing the array passing the array as an explicit pointer... Any idea qhat could be causing this ?
Thanks a lot for the help
Code:
#include <stdio.h>
#define NOERRORS 0 /* no errors found */
#define MAX_NUM_SUITS 4 /* maximum number of suits */
#define MAX_NUM_FACES 13 /*maximum number of faces */
#define TOTAL_CARDS_IN_DECK 52 /* total cards in one deck */
#define CODE_CLUB "\xe2\x99\xa3" /* club char UTF-8 */
#define CODE_DIAMOND "\xe2\x99\xa6" /* diamond char UTF-8 */
#define CODE_HEART "\xe2\x99\xa5" /* heart char UTF-8 */
#define CODE_SPADE "\xe2\x99\xa0" /* spade char UTF-8 */
/* program enumerators */
enum CARD_FACES { ACE = 1, TWO, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING };
enum CARD_SUITS { CLUB, DIAMOND, HEART, SPADE};
/* program structs */
struct card {
int face;
int suit;
};
/* function prototypes */
void cardMake(struct card, int, int );
void cardPrint(struct card );
void deckMake(struct card *);
/* main program */
int main(int argc, char *argv[])
{
struct card myDeck [TOTAL_CARDS_IN_DECK];
deckMake(myDeck);
int i = 0;
for (i = 0; i < TOTAL_CARDS_IN_DECK; i++) {
cardPrint(myDeck[i]);
}
printf("\n");
return NOERRORS;
}
/* Card print function*/
void cardPrint(struct card myCard){
const char * STR_FACES [] = {" "," A"," 2"," 3"," 4",
" 5"," 6", " 7"," 8"," 9",
"10"," J"," Q"," K"};
const char * STR_SUIT_CODE [] = {CODE_CLUB, CODE_DIAMOND,
CODE_HEART, CODE_SPADE };
printf("%s%s", STR_FACES[myCard.face], STR_SUIT_CODE[myCard.suit]);
}
/* Card make function */
void cardMake(struct card myCard, int face, int suit){
myCard.face = face;
myCard.suit = suit;
}
/* DeckMake Function */
void deckMake(struct card deck [] ){
struct card tempCard;
int indexDeck = 0;
int indexSuit = 0;
for (indexSuit = 0; indexSuit < MAX_NUM_SUITS; indexSuit++){
int indexFace = 0;
for (indexFace = 0; indexFace < MAX_NUM_FACES; indexFace++){
cardMake(tempCard, indexFace, indexSuit);
deck[indexDeck] = tempCard;
}
}
}