Thread: seg fault

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005

    seg fault

    Hey guys this is my whole program and i get a segmentation fault when i do
    this menu->headCategory->headItem->prices[i].dollars = 0; how am i going outside
    the bounds of the array . NUM_PRICES is equal to 3 how am i exceeding that

    void systemInit(GJCType* menu)
       int i;
       for(i=0; i<NUM_PRICES ; i++)
         menu->headCategory->headItem->prices[i].dollars = 0;
         menu->headCategory->headItem->prices[i].cents = 0;
       menu->headCategory->numItems = 0;
    #ifndef GJC_H
    #define GJC_H
    /* System-wide header files. */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* System-wide constants. */
    #define ID_LEN 5
    #define MIN_NAME_LEN 1
    #define MAX_NAME_LEN 25
    #define MIN_DESC_LEN 1
    #define MAX_DESC_LEN 250
    #define NUM_PRICES 3
    #define HOT 'H'
    #define COLD 'C'
    #define ERRORCODE 1
    #define VALID 0
    typedef struct category* CategoryTypePtr;
    typedef struct item* ItemTypePtr;
    /* Structure definitions. */
    typedef struct price
       unsigned dollars;
       unsigned cents;
    } PriceType;
    typedef struct item
       char itemID[ID_LEN + 1];
       char itemName[MAX_NAME_LEN + 1];
       PriceType prices[NUM_PRICES];
       char itemDescription[MAX_DESC_LEN];
       ItemTypePtr nextItem;
    } ItemType;
    typedef struct category
       char categoryID[ID_LEN + 1];
       char categoryName[MAX_NAME_LEN + 1];
       char drinkType;      /* (H)ot or (C)old. */
       char categoryDescription[MAX_DESC_LEN];
       CategoryTypePtr nextCategory;
       ItemTypePtr headItem;
       unsigned numItems;
    } CategoryType;
    typedef struct gjc
       CategoryTypePtr headCategory;
       unsigned numCategories;
    } GJCType;
    int commandLineArguments(int argc, char* argv[]);
    #include "gjc.h"
    #include "gjc_options.h"
    #include "gjc_utility.h"
    int main(int argc, char* argv[])
       GJCType menu;
       int cmd;
       return EXIT_SUCCESS;
    int commandLineArguments(int argc, char *argv[])
        /* declare file pointers to files*/
       FILE *menu;
       FILE *submenu;
       /* command line argument*/
       /* checks to see if 3 command line arguments have been entered*/
         printf("Invalid: Enter 3 command line arguments (Error:)\n");
       else if(argc >3)
         printf("Too many arguments where supplied\n");
         return ERRORCODE;
       /* opening the menu.dat file for reading*/
       menu = fopen(argv[1], "r");
       /* check to see if the file exists*/
       if(menu ==NULL)
          printf("Im sorry %s does not exit(ERROR!)\n", argv[1]);
          return ERRORCODE; /* cannot proceed*/
       /* opening the submenu file for reading*/
       submenu = fopen(argv[2], "r");
       /* check to see if the subMenu file exists*/
       if(submenu ==NULL)
          printf("The file %s does not EXIST(ERROR!)\n", argv[2]);
          return ERRORCODE; /* cannot proceed*/
       if(fclose(menu)!=0 || fclose(submenu)!=0)
          fprintf(stderr, "Error in closing files\n");
       return EXIT_SUCCESS;

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    It doesn't have to do with array bounds, it has to do with pointers not being initialized.

    Nowhere do you initialized menu->headCategory. So when you try to dereference it you get the seg fault. You'll need to malloc() some memory for your pointers.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM