Thread: Array of Structures Segmentation Fault

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    36

    Array of Structures Segmentation Fault

    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;
    
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Turning up the warning level with gcc -W -Wall gives the following warning which hints at the problem:
    Code:
    cb_array_of_structures.c: In function ‘cardMake’:
    cb_array_of_structures.c:65:27: warning: parameter ‘myCard’ set but not used [-Wunused-but-set-parameter]
     void cardMake(struct card myCard, int face, int suit){
                               ^
    It's saying that the setting of myCard's fields is useless. You probably want a pointer here so that setting the fields can affect the caller's struct:
    Code:
    void cardMake(struct card *myCard, int face, int suit){
        myCard->face = face;
        myCard->suit = suit;
    }
    // Call it like:
        cardMake(&tempCard, indexFace, indexSuit);
    You have a two other errors:
    * You're not incrementing indexDeck.
    * You're starting indexFace at 0 but it should start at 1

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    36
    Hi algorism,

    Thanks a lot for your help. I did all you recommended and still I was getting errors. It turns out that the whole problem was caused by that one offset between the face and the face enumerator print.

    this is the updated code. Note that I got rid the pointers and now I am just using arrays or copy of the structures which are not that big so performance should be fine.

    Code:
    #include <stdio.h>
    #include <stddef.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 */
    struct card  cardMake(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);
    
    
        /* Test to make sure deck is working */
        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 };
    
        char * format;
        if (myCard.face == 10){
            format = (char *) "  %s%s";
        }else {
            format = (char *) " %s%s";
        }
    
        printf(format, STR_FACES[myCard.face], STR_SUIT_CODE[myCard.suit]);
    
    }
    
    
    /* Card make function */
    struct card cardMake(int face, int suit){
    
        struct card temp;
    
        temp.face = face;
        temp.suit = suit;
        return temp;
    
    }
    
    /* DeckMake Function */
    void deckMake(struct card * myDeck){
    
        int indexDeck = 0;
        int indexSuit =0;
        int indexFace = 0;
    
        for (indexSuit = 0; indexSuit < MAX_NUM_SUITS; indexSuit++){
            for (indexFace = 0; indexFace < MAX_NUM_FACES; indexFace++){
                myDeck[indexDeck++] = cardMake(indexFace + 1 ,indexSuit);
            }
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array segmentation fault
    By Max2011 in forum C Programming
    Replies: 1
    Last Post: 05-21-2011, 10:30 AM
  2. Segmentation fault while using 2D array
    By Damon Dike in forum C Programming
    Replies: 7
    Last Post: 04-03-2010, 08:23 AM
  3. Replies: 3
    Last Post: 03-03-2010, 03:35 PM
  4. Segmentation fault when declaring 2 structures
    By Kleid-0 in forum C Programming
    Replies: 9
    Last Post: 12-15-2004, 06:45 PM
  5. Segmentation fault with array?
    By whiphub in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2004, 01:51 PM

Tags for this Thread