Hey guys i did a backtrace using gdb and im getting a segfault when i call checkduplication function in my load function. i get the segfault here where the strcmp is i have no idea why? Am i trying to access something that isnt there im not sure? current = current->nextCategory; Also am i doing the validation correctly for each malloc? Code: /* checks for duplication of fields the catID field*/ int checkDuplicationID(GJCType *menu, char* catID) { CategoryTypePtr current; current = menu->headCategory; while(current !=NULL) { if(strcmp(current->categoryID, catID) == ERRORCODE) { printf("A duplicate record was found error:\n\n"); return ERRORCODE; } /* i get a seg fault on this next line*/ current = current->nextCategory; } return 1; } Code: int loadData(GJCType* menu, char* menuFile, char* submenuFile) { /*pointer for malloc*/ GJCType *newP; /*declaration of variables*/ int CountNoLines = 0; char temp[LINE_LENGTH]; /* variables for tokenizer for menu file*/ char *catID; char *catType; char *catName; char *catDescription; /* declare file pointers to files*/ FILE *f1; FILE *f2; /* opening the menu and submenu for reading*/ f1 = fopen(menuFile, "r"); f2 = fopen(submenuFile, "r"); /* check to see if the file exists*/ if(f1 ==NULL || f2 == NULL) { /* check whether both files have existed or typed in correctly*/ if(f1 == NULL && f2 == NULL) { printf("Both files do not exist %s %s\n",menuFile,submenuFile); } /* check for each file existance*/ else if (f1 == NULL) { printf("%s does not exist\n\n",menuFile); } /* check for each file existance*/ else if(f2 == NULL) { printf("%s does not exist\n\n", submenuFile); } printf("EXITING PROGRAM ERROR!\n\n"); return ERRORCODE; /* cannot proceed*/ } /* counts how many fields there are in the file*/ while(fgets(temp, LINE_LENGTH, f1)!=NULL) { temp[strlen(temp)]= '\0'; printf("%s", temp); if(!countToken(f1, temp, TOKEN_PRODUCT)) { return ERRORCODE; } CountNoLines++; } printf("%d", CountNoLines); /* set f1 pointer to the start of the file for reading*/ fseek(f1, 0, SEEK_SET); /* if there is more than 0 lines tokenize fields check if there is duplication and check the string length and if its too big its the wrong data format */ if(CountNoLines > 0) { while(fgets(temp, LINE_LENGTH, f1) !=NULL) { /* stores the catID but also checks if there is a duplicate*/ catID = strtok(temp, "|"); /* checking whether it tokenizez*/ if(catID == NULL) { printf("CatID missing\n"); } /*checks for a duplication of the id field*/ if(!checkDuplicationID(menu, catID)) { return ERRORCODE; } /* gets the second field and tokenizez it*/ catType = strtok(NULL, "|"); /* checking whether it tokenizez*/ if(catType == NULL) { printf("CatType missing\n"); } catName = strtok(NULL, "|"); /* checking whether it tokenizez*/ if(catName == NULL) { printf("CatName missing\n"); } catDescription = strtok(NULL, "\0"); /* checking whether it tokenizez*/ if(catDescription == NULL) { printf("CatDescription missing\n"); } /* checks the string length of the field*/ if((strlen(catID) >ID_LEN) || (strlen(catType) >MAX_NAME_LEN) || (strlen(catDescription) >MAX_DESC_LEN)) { printf("Wrong data format\n\n"); return ERRORCODE; } /* malloc the pointers*/ newP = malloc(sizeof (GJCType)); /* check if it cannot allocate memory*/ if(newP == NULL) { printf("Memory could not be allocated\n\n"); return ERRORCODE; } /* malloc the pointers*/ newP->headCategory = malloc(sizeof(CategoryType)); /* check if it cannot allocate memory*/ if(newP == NULL) { printf("Memory could not be allocated\n\n"); return ERRORCODE; } /* malloc the pointers*/ newP->headCategory->headItem = malloc(sizeof(ItemType)); /* check if it cannot allocate memory*/ if(newP == NULL) { printf("Memory could not be allocated\n\n"); return ERRORCODE; } /* malloc the pointers*/ newP->headCategory->nextCategory = malloc(sizeof(CategoryType)); /* check if it cannot allocate memory*/ if(newP == NULL) { printf("Memory could not be allocated\n\n"); return ERRORCODE; } /* malloc the pointers*/ newP->headCategory->headItem->nextItem =malloc(sizeof(ItemType)); /* check if it cannot allocate memory*/ if(newP == NULL) { printf("Memory could not be allocated\n\n"); return ERRORCODE; } /*copy variables into structure*/ strcpy(newP->headCategory->categoryID, catID); newP->headCategory->drinkType = *catType; strcpy(newP->headCategory->categoryName, catName); strcpy(newP->headCategory->categoryDescription, catDescription); newP->headCategory->nextCategory = NULL; } } /* close both files after reading*/ if(fclose(f1)!=0 || fclose(f2)!=0) { fprintf(stderr, "Error in closing files\n"); } return EXIT_SUCCESS; } /* my structs header file*/ 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 #define LINE_LENGTH 300 #define TOKEN_PRODUCT 4 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[]); int countToken(FILE *fp, char* temp, int tokenPerLine); #endif Code: #include "gjc.h" #include "gjc_options.h" #include "gjc_utility.h" int main(int argc, char* argv[]) { /* declaration of variables*/ GJCType menu; int cmd; char *menuFile; char *submenuFile; int openData; /* to check how many command line aguments entered*/ commandLineArguments(argc,argv); /* populating arguments with pointers to files*/ menuFile = argv[1]; submenuFile = argv[2]; /*initialize variables and pointers*/ /*systemInit(&menu);*/ /* populate the data in the files into memory*/ openData = loadData(&menu, menuFile, submenuFile); /* if it does not open release the memory*/ if(!openData) { systemFree(&menu); return EXIT_SUCCESS; } return EXIT_SUCCESS; } int commandLineArguments(int argc, char *argv[]) { /* 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; } return EXIT_SUCCESS; }
It looks like the current->categoryID is not properly initialized and it does not contain the proper data.
The problem is with the menu variable. You are assigning current as menu->headCategory where menu is passed through the function loadData where menu is also passed and so loadData is called from the main function and in main function where you initialized the menu is commented. See line /*systemInit(&menu);*/ and so the menu is never initialized giving you segmentation fault. I hope this clears your query.