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
Code:
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;
menu->numCategories=0;
}
Code:
#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[]);
#endif
Code:
#include "gjc.h"
#include "gjc_options.h"
#include "gjc_utility.h"
int main(int argc, char* argv[])
{
GJCType menu;
int cmd;
commandLineArguments(argc,argv);
systemInit(&menu);
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*/
if(argc<3)
{
printf("Invalid: Enter 3 command line arguments (Error:)\n");
exit(ERRORCODE);
}
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;
}