sorting list gettin seg fault

This is a discussion on sorting list gettin seg fault within the C Programming forums, part of the General Programming Boards category; Hey guys im doing my sorting routine for linked list but now im obviouly accessing something i shouldnt and im ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    sorting list gettin seg fault

    Hey guys im doing my sorting routine for linked list but now im obviouly accessing something i shouldnt and im getting a segment fault like usual
    can somebody help me please?



    Code:
    /****************************************************************************
    * Loads all data into the system.
    ****************************************************************************/
    int loadData(GJCType* menu, char* menuFile, char* submenuFile)
    {
    
       drinkType *currentCat, *prevCat, *newCat, *begin, *end, *var;
       ItemType *currentItem, *prevItem, *newItem;
       
       FILE *fp1;
       FILE *fp2;
       
       char *token, *line;
       char array[BUFFER_SIZE + 2];
       double d;
       unsigned doll;
       int i;
       char nc_array[NC_ARRAY_SIZE + 1];
       
       /* Open menu file for reading. */
       fp1 = fopen(menuFile, "r");
       
       /* check if fp1 menu file exists*/
       if(fp1 == NULL)
       {
          printf("file %s does not exist \n", menuFile);
          exit(0);
          
       }
       
       /* initialize the previous node to NULL*/
       /*prevCat = NULL;*/
       
       while((line = fgets(array, BUFFER_SIZE + 2, fp1)) != NULL)
       {
          /* allocate memory for CategoryType pointer*/
          newCat = malloc(sizeof(drinkType));
          
          /* check if memory allocation succeeded if fails exit*/
          if(newCat == NULL)
          {
              printf("Memory Allocation error for newCat\n");
    	  exit(0);
          }
          
          /* if the prevCat is NULL point the new node to the 
          start of the list*/
        /*  if(prevCat == NULL)
          {
             menu->headCategory = newCat;
          }*/
          /* if it isnt at the start get the next -1node*/
        /*  else
          {
             prevCat->nextCategory = newCat;
          }*/
           
          
          /* tokenize the Pipe and copy the field
          pointers into the struct*/
               
          token = strtok(line, "|");
          strcpy(newCat->categoryID, token);
               
          token = strtok(NULL, "|");   
          newCat->drinkType = token[0];
          
          token = strtok(NULL, "|");
          strcpy(newCat->categoryName, token);
            
          token = strtok(NULL, "|");
          strcpy(newCat->categoryDescription, token);
       
          /* initialize everything to a safe state*/
          /*newCat->nextCategory = NULL;
          newCat->headItem = NULL;
          newCat->numItems = 0;
          prevCat = newCat;*/
    
          var->nextCategory = NULL;
          var->headItem = NULL;
          var->numItems = 0;
          
       }
       
       /* the current pointer points to headCategory*/
       currentCat = menu->headCategory;
       addSortedDrink(menu, begin, &end, &var);
       
       /* for testing purposes traverse through the list
       and print out linked list*/
       /*
       while(currentCat != NULL)
       {
          printf("\n%s, %c, %s, %s", currentCat->categoryID, 
          currentCat->drinkType,
          currentCat->categoryName, 
          currentCat->categoryDescription);
        */  
          /* get the next row
          currentCat = currentCat->nextCategory;
       }*/
       
       /* close the first pointer to the file*/
       /* check if it can close it otherwise error*/
       if(fclose(fp1)!=0)
       {
           fprintf(stderr, "cannot close file\n");
       }
       
       
       /* Open submenu file for reading. */
       fp2 = fopen(submenuFile, "r");
       
       /* check if sub menu file exists*/
       if(fp2 == NULL)
       {
          printf("file %s does not exist \n", submenuFile);
          exit(0);
          
       }
       /*intialize pointer to the start*/
       prevItem = NULL;
       currentCat = menu->headCategory;
       
       while((line = fgets(array, BUFFER_SIZE + 2, fp2)) != NULL)
       {
          newItem = malloc(sizeof(ItemType));
        
          token = strtok(line, "|");
          strcpy(newItem->itemID, token);
          
          token = strtok(NULL, "|");
          strcpy(nc_array, token);
           
          /* put the current pointer to the head of the list*/
          currentCat = menu->headCategory;
          
          /* while current is pointing to a node*/
          
          while(currentCat != NULL)
          {
             if(strcmp(currentCat->categoryID, nc_array) == 0)
    	 {
    	    break;
    	 }
    	 
    	 currentCat = currentCat->nextCategory;
          }
    	 
    	 if(currentCat == NULL)
    	 {
    	    printf("NULL POINTER Error!!!\n");
    	 } 
    	 else
    	 {
                token = strtok(NULL, "|");
                strcpy(newItem->itemName, token);     
                
    	    /* store dollars into struct array*/
    	    /* convert all prices to float when storing*/
                for(i = 0; i < NUM_PRICES; i++)
                { 
                   token = strtok(NULL, "|");
                   d = atof(token); 
                   doll = (unsigned) d;  
                   newItem->prices[i].dollars = doll;
                   newItem->prices[i].cents = (d - doll) * 100;
                }
          
                token = strtok(NULL, "|");
                strcpy(newItem->itemDescription, token);
          
                /* from the start of the list*/
                newItem->nextItem = currentCat->headItem;
                currentCat->headItem = newItem;
    	    
    	    /* increment the counter*/
    	    currentCat->numItems ++;
    	 }
          
       }
       currentCat = menu->headCategory;
       /*
       while(currentCat!= NULL)
       {
          currentItem = currentCat->headItem;
          printf("Items for category %s\n", currentCat->categoryID);
       
          while(currentItem != NULL)
          {
             printf("\n%s, %s, %s, %d.%d, %d.%d, %d.%d, %s\n", 
             currentItem->itemID, currentCat->categoryID,
             currentItem->itemName, currentItem->prices[0].dollars,currentItem->prices[0].cents,
             currentItem->prices[1].dollars, currentItem->prices[1].cents,
             currentItem->prices[2].dollars, currentItem->prices[2].cents,
             currentItem->itemDescription);
             currentItem = currentItem->nextItem;
          }
          
          currentCat = currentCat->nextCategory;  
       }*/
       /* try to close the file*/
       /* if it wont close provide an error*/
       if(fclose(fp2)!=0)
       {
           fprintf(stderr, "cannot close file\n");
       }
       
       
       return EXIT_SUCCESS;
    
    
    }
    
    
    
    void addSortedDrink(GJCType *menu, 
                        drinkType *i, 
    		    drinkType **start,
    		    drinkType **last)
    {
    
          drinkType *old, *p;
          p = *start;
          
          if(!*last)
          {
             i->nextCategory = NULL;
    	 *last = i;
    	 *start = i;
    	 return;
          
          }
          
          old = NULL;
          
          while(p)
          {
             if(strcmp(p->categoryName, i->categoryName)<0)
    	 {
    	     old = p;
    	     p = p->nextCategory;
    	 
    	 }
    	 else
    	 {
    	    if(old)
    	    {
    	       old->nextCategory = i;
    	       i->nextCategory = p;
    	       return;
    	    
    	    }
    	    i->nextCategory = p;
    	    *start = i;
    	    return;
    	 
    	 }
          
          }
          (*last)->nextCategory = i;
          i->nextCategory = NULL;
          *last = i;
    
    }
    
    ypedef 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;
    } drinkType;
    
    typedef struct gjc
    {
       CategoryTypePtr headCategory;
       unsigned numCategories;
    } GJCType;
    
    int commandLineArguments(int argc, char *argv[]);

  2. #2
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    sorry guys just figured out what it was. But now i get a seg fault when im trying to call

    addSortedDrink(menu, begin, &end, &var);

    am i not passsing the variables properly im not sure

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Another List Q - Seg fault on head-tail list
    By JimpsEd in forum C Programming
    Replies: 11
    Last Post: 05-10-2006, 12:53 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Replies: 6
    Last Post: 03-02-2005, 01:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21