Thread: problem with structures and linked list

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    problem with structures and linked list

    I can't figure out what is wrong with my code. I get no compiler errors, but my program crashes when a number is entered for "Please enter result # for Experiment name:"

    Can anyone figure out what I am doing wrong? Please help!

    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <malloc.h>
    
    struct Result {
    float value;                   /*  value of result */
    struct Result *pt_res_next;
    };
    
    
    struct Exp {
    char ExpName[10];           
    struct Result *pt_res;
    struct Exp *pt_exp_next;
    };
    
    
    /* Declare the list node to tie all the Experiment structs together into 
       a list. 
    */
       struct ExpList 
    	{
    		struct Exp* pExp;
    		struct ExpList* value;
    	};
    
    struct Exp* GetExp()
    /*
       Assumes: ???????
       Results: Allocates a new Experiment structure and asks the user for the
                values to fill the Experiment fields (Experiment name and result number).
       Returns: A pointer to a new struct Experiment*. It is the responsibility
                of the caller to delete this struct.
    */
    {
    	int i; 
       int ExpResult;
       
       struct Exp* pNewExp;
       struct Result* pNewRes;
       
       pNewExp = (struct Exp*)malloc(sizeof(struct Exp));
       printf("How many results for Experiment? :");
       scanf("%d", &ExpResult);
       getchar();/* this getchar eats the return left by the scanf. */
    
       for (i = 0; i < ExpResult; ++i)
       {
    		printf("Please enter the name of the Experiment:");
    		gets(pNewExp->ExpName);
    		pNewRes = (struct Result*)malloc(sizeof(struct Result));
    		printf("Please enter result %d for Experiment %s :", i + 1, pNewExp->ExpName);
    
    
    	/*	pNewValue = (struct Result*)malloc(sizeof(struct Result));   */
    		scanf("%f", pNewRes->value);
    		getchar();/* this getchar eats the return left by the scanf. */
       }
       return pNewExp;
       
    }
    
    void PrintExp(struct Exp* pExp)
    /*
       Assumes: ???????
       Results: Prints the name and value number fields of the Experiment struct
       Returns: none
    */
    {
       printf("Experiment name%s\n", pExp->ExpName);
    
    
    
       printf("Result: %f\n", pExp->pt_res);
    }
    
    void PrintExpList(struct ExpList* pList)
    /*
       Assumes: ???????
       Results: Prints the name and phone number for every Experiment in the list 
                pList. The Experiment records are seperated on the screen by a 
                blank line.
       Returns: none
    */
    {
       /* The first element in the list is a special case */
       PrintExp(pList->pExp);
    
       while (pList->value)
       {
          pList = pList->value;
          PrintExp(pList->pExp);
       }
    }
    
    void DeleteExp(struct Exp* pExp)
    /*
       Assumes: ???????
       Results: frees all memory allocated for the Experiment struct pointed to
                by pEmp.
       Returns: none
    */
    {
       free(pExp);
    }
    
    void DeleteExpList(struct ExpList* pList)
    /*
       Assumes: ???????
       Results: Deletes every Experiment in the list pList and every ExperimentList
                struct except the first one.
       Returns: none
    */
    {
       struct ExpList* pCursor;
    
       /* we have to treat the first node in the list as a special case */
       DeleteExp(pList->pExp);
       pList = pList->value;
    
       while (pList)
       {
          DeleteExp(pList->pExp);
          pCursor = pList;
          pList = pList->value;
    
          free(pCursor);
       }
    }
    
    int main()
    {
       /* this is our list handle for the duration of main() */
       struct ExpList list;
    
       /* a utility pointer to help us build the list */
       struct ExpList* pListTail; 
    
       /* a utility pointer for new list nodes */
       struct ExpList* pNewListElement; 
    
       /* a utility pointer for new data elements */
       struct Exp* pNewExp; 
    
       /* declare some variables for the control logic */
       int nNumExps;
       int i;
    
       /* Do some initialization to make our list safe (and empty) */
       list.pExp = NULL;
       list.value = NULL;
    
       /* Ask the user how many Exps to enter into the list */
       printf("How many Experiments are there?: ");
       scanf("%d", &nNumExps);
       getchar();/* this getchar eats the return left by the scanf. */
    
       /* Now for each Experiment get a new record and add it to the list. */
       for (i = 0; i < nNumExps; i++)
       {
          /* Get a new data object from the user */
          pNewExp = GetExp();
    
          if (list.pExp == NULL)
          {
             /* This is the first element in the list... */
             list.pExp = pNewExp;
             pListTail = &list;
          }
          else
          {
             /* there are already elements in the list (at least one), 
                so add this one to the end */
    
             /* Step one is to make a new list node */
             pNewListElement = (struct ExpList*)malloc(sizeof(struct ExpList));
    
             /* Now hook that new list node up to the end of the list */
             pListTail->value = pNewListElement;
    
             /* then move the tail marker to the new last node */
             pListTail = pNewListElement;
    
             /* Now make sure the new last node's next points to NULL so we can 
                find the end of the list */
             pListTail->value = NULL;
    
             /* and lastly, make the new last node point at the new Experiment 
                record. */
             pListTail->pExp = pNewExp;
          }
       }
    
       /* Print the list of Experiments out to the screen */
       PrintExpList(&list);
    
       /* The last thing we need to do before we exit is to free all the memory we 
          allocated. */
       DeleteExpList(&list);
    
       return 0;
    }
    Tagged by Salem

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Please edit your post and apply code tags. It makes it much easier to read.
    [code]
    ...Your code here...
    [/code]
    That way it looks nice and formatted.

    You shouldn't use gets. It's prone to buffer overflow which likes to crash programs.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well since Quzah asked so nice and I'm not seeing code tags yet, I'll just add that you would use fgets in gets' place.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    I get an error message for "too few parameters" with fgets. What parameters am I suppose to add here? Some code would help, please help. I have never used fgets before and I know very little about it.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    fgets - read the BUGS section.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    I still do not understand how I am suppose to use fgets with structures - What parameters am I missing if I replace the "gets" with "fgets"? Am I missing a variable that I need to add for fgets to work in my program? Please help!

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    Thanks Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list
    By tallgeese84 in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:51 PM
  2. C programming-Linked list problem
    By male4329 in forum C Programming
    Replies: 18
    Last Post: 06-02-2005, 02:05 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Linked List Problem
    By animeaholic in forum C Programming
    Replies: 1
    Last Post: 12-09-2002, 06:36 PM