Thread: segmentation fault - pointers/functions

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    Question segmentation fault - pointers/functions

    I'm having a problem in my second performed function. After the first 'hours' are entered I get a 'segmentation fault'. Any thoughts?

    Code:
    #include <stdio.h> 
    #include <strings.h>
    #include <stdlib.h>
    
    #define total_employees 5
    #define over_time_rate  1.5
    #define forty_hour_week 40
    
    struct s_employee
    {
        char  name[20]       ;   /* user name              */ 
        int   clock_number   ;   /* clock #                */
        float hourly_rate    ;   /* hourly rate of pay     */
        float hours_worked   ;   /* hours worked           */           
        float overtime_hours ;   /* OT hours worked        */
        float gross_pay      ;   /* calc gross pay amount  */
        float forty_hour_pay ;   /* calc gross pay amount for 40 hours        */
        float overtime_pay   ;   /* calc gross pay amount for time > 40 hours */
    
        struct s_employee *next_emp;    
    };
    
    
    
    
    
    /************************************************************************/
    /*              begin - Function: f_input1                              */
    /************************************************************************/
    /*                                                                      */
    /*  Purpose:    Obtains input from user                                 */   
    /*                                                                      */
    /*  Parameters: emp1 - ptr to linked list                               */
    /*                                                                      */
    /*  Returns:    Nothing                                                 */
    /*                                                                      */
    /************************************************************************/
    
           void f_input1 (void)
    
           {
    
        /* local variable declaration                                   */ 
               int  f_counter                = 0    ;   
               int  f_clock_number           = 0    ;   
               char f_first_name [20]        = ""   ;
               char f_last_name  [20]        = ""   ;
               float f_hourly_rate           = 0    ; 
    
      
        /* misc pointers                                                */
               struct s_employee *current_pointer, *initial_pointer, *emp1 ;
    
        /* allocate memory                                              */
               initial_pointer = (struct s_employee *)malloc(sizeof(struct s_employee));
    
        /* Set current_pointer item to point to the first item.         */
               current_pointer = initial_pointer;                 
    
        /* populate struct                                              */
               while ( f_counter < total_employees )   
    
    
               { 
              
    
        /* Prompt for user to input their first name / last name        */
        /* concat first/last name and move to array struct, copy string */
               printf ("Please enter the first name: ");
               scanf  ("%s", f_first_name);
    
               printf ("Please enter the last name: ");  
               scanf  ("%s", f_last_name);
    
               strcat (f_first_name, " ");
               strcat (f_first_name, f_last_name);            
               strcpy(current_pointer->name, f_first_name);  
             
    
        /* Prompt for user to input the clock #                         */
               printf ("Please enter the clock number: ") ;           
               scanf  ("%f", &current_pointer->clock_number);
    
               
        /* Prompt for user to input the hourly rate of pay              */
               printf ("Please enter the hourly rate: ") ;           
               scanf  ("%f", &current_pointer->hourly_rate);
    
        
        /* process next_emp                                             */
               current_pointer = current_pointer->next_emp;
               current_pointer = (struct s_employee *)malloc(sizeof(struct s_employee));
               ++f_counter;
    
               }        
              
           } 
    
    /************************************************************************/
    /*                end - Function: f_input1                              */
    /************************************************************************/
    
    
    /************************************************************************/
    /*              begin - Function: f_input2                              */
    /************************************************************************/
    /*                                                                      */
    /*  Purpose:    Obtains hours worked from user                          */
    /*                                                                      */
    /*  Parameters: emp1 - ptr to linked list                               */
    /*                                                                      */
    /*  Returns:    Nothing                                                 */
    /*                                                                      */
    /************************************************************************/
    
           void f_input2 (void)
           {
    
        /* local variable declaration                                   */ 
               int  f_counter            = 0    ;       
    
        /* misc pointers                                                */
               struct s_employee *current_pointer, *initial_pointer, *emp1 ;
               
        /* Set current_pointer item to point to the first item.         */
               current_pointer = initial_pointer;
    
        /* populate struct                                              */
               while ( f_counter < total_employees )   
    
               { 
    
    
        /* Prompt for user to input the clock hours                     */
               printf ("Please enter the total hours worked: ") ;           
               scanf  ("%f", &current_pointer->hours_worked);
    
               printf ("\n"); 
    
        /* process next_emp                                             */     
               current_pointer = current_pointer->next_emp;
               ++f_counter;  
    
               }        
              
           } 
    
    /************************************************************************/
    /*                end - Function: f_input2                              */
    /************************************************************************/
    
    
    
    
    
    main()
    {
    
         /* create a linked list                                                */ 
               struct s_employee emp1, emp2, emp3, emp4, emp5;
    
               emp1.next_emp = &emp2;        
               emp2.next_emp = &emp3;        
               emp3.next_emp = &emp4;        
               emp4.next_emp = &emp5;        
               emp5.next_emp = ( struct s_employee *) 0;        
    
    
    
               
        /********************** begin performed functions ***********************/
    
        /* Explain what we will be processing                                   */
               f_explain_message (); 
        
    
        /* perform get misc function                                            */
               f_input1 ();
    
    
        /* perform get hours function                                           */ 
               f_input2 ();
    
    
        /* perform grosspay calc function                                       */
               f_gross_pay_calc ();
     
    
        /* perform print headings function                                      */    
               f_print_headings ();
      
    
        /* perform Print outstruct employee information to the screen function  */ 
               f_print_detail ();
    
    
        /********************** end performed functions *************************/
     
    
    
    
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's one thing:

    >>int clock_number;
    >scanf("%f", &current_pointer->clock_number);
    You declared clock_number as an int, but used %f to read into it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    Thanks Hammer. Changed that from f to i but I still have the same error.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Try flushing the input buffer after calling scanf().

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    If I do that won't I lose all previously entered data that's in memory?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    Lightbulb

    I added the flush but still having problems. Perhaps I'll attempt to debug or ...


    Code:
    /************************************************************************/
    /*              begin - Function: f_input2                              */
    /************************************************************************/
    /*                                                                      */
    /*  Purpose:    Obtains hours worked from user                          */
    /*                                                                      */
    /*  Parameters: emp1 - ptr to linked list                               */
    /*                                                                      */
    /*  Returns:    Nothing                                                 */
    /*                                                                      */
    /************************************************************************/
    
           void f_input2 (void)
           {
    
        /* local variable declaration                                   */ 
               int  f_counter            = 0    ;       
    
        /* misc pointers                                                */
               struct s_employee *current_pointer, *initial_pointer, *emp1 ;
               
        /* Set current_pointer item to point to the first item.         */
               current_pointer = initial_pointer;
    
        /* populate struct                                              */
               while ( f_counter < total_employees )   
    
               { 
    
    
        /* Prompt for user to input the clock hours                     */
               printf ("Please enter the total hours worked: ") ;           
               scanf  ("%f", &current_pointer->hours_worked);
    
               printf ("\n"); 
    
               for (;getchar() != '\n';); // This flushes the buffer.
    
        /* process next_emp                                             */     
               current_pointer = current_pointer->next_emp;
               ++f_counter;  
    
               }        
              
           } 
    
    /************************************************************************/
    /*                end - Function: f_input2                              */
    /************************************************************************/

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    Question

    Not sure of the proper format to return a linked list. I've not found any good examples. Is this right?

    /* return a linked list from f_input1 function */
    return (struct s_employee *next_emp);


    My next issue is how to reference the first entry in the linked list in my next function so I can add hours. Would this be right?

    /* perform get hours function */
    f_input2 (struct s_employee *next_emp);

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    Question

    Salem,

    Thanks for your help. The code works nicely with 2 minor exceptions:

    1. I had to move

    struct s_employee *emps;

    to the top of the pgm because of compile errors.

    2. When function input2 gets invoked, I had to press the 'enter'
    key to get the user prompting to display. Is there an easy
    way around this?
    Last edited by p1c1078; 04-20-2003 at 01:50 PM.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I had to press the 'enter' key to get the user prompting to display. Is there an easy way around this?
    Use fflush(stdout); after the printf().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    Thanks Hammer! Works great!!

  11. #11
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Well - two small problems which I didn't notice first time round
    scanf("%f", &emp->clock_number);
    should be
    scanf("%d", &emp->clock_number);
    Hammer Here's one thing:

    >>int clock_number;
    >scanf("%f", ¤t_pointer->clock_number);
    You declared clock_number as an int, but used %f to read into it.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    Question

    fyi - Using a UNIX based compiler here.

    As was done in the code Salem added for 'f_input1' function I also need to return a pointer to a subsequent function from the 'f_input2' function. I'm not sure how to pass this correctly.


    Code:
            void f_input2 (struct s_employee *emps)
       
            {
    
            while ( emps != NULL )
    
                {
    
                printf("Please enter the total hours worked by %s: ",  
                         emps->name );
                scanf("%f", &emps->hours_worked);
                printf("\n");
                emps = emps->next_emp;
    
                }
    
        /* return pointer to the start of the list                     */
              return ????;
    
    
            }

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>return pointer to the start of the list
    To do this, just have a local pointer that you don't change, or use a local pointer to do the stepping through the list.

    Code:
    void f_input2 (struct s_employee *emps)
    {
      struct s_employee *tmp = emps;
      
      while (tmp)
      {
          /* Do stuff */
         tmp = tmp->next_emp;
      }
    
      return emps;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    Question

    Hammer,

    I get the following UNIX compile error:

    g11.c: In function `f_input2':
    g11.c:210: warning: `return' with a value, in function
    returning void





    Code:
            void f_input2 (struct s_employee *emps)  
    
             
            {
    
        /* misc pointers                                                */
            struct s_employee *tmp = emps;
    
    
    
            while ( tmp != NULL )
    
                {
    
                printf("Please enter the total hours worked by %s: ", tmp->name );
                scanf("%f", &tmp->hours_worked);
                printf("\n");
                tmp = tmp->next_emp;
    
                }
    
        /* return pointer to the start of the list                     */
              return emps;
    
    
            }

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Yeah, errmm, my mistake

    >>void f_input2 (struct s_employee *emps)
    should be
    >>struct s_employee* f_input2 (struct s_employee *emps)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM