Thread: segFault

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    segFault

    Code:
    void hire(FILE **infile, char lineOfText[], struct employee **firstEmployee)
    {
      struct employee *newEmployee = NULL;
      struct employee *previousEmployee = NULL;
      struct employee *currentEmployee = NULL;
      
      read(*infile, lineOfText);
      while(lineOfText[ZEROI] != '*')
      {
    	allocate(&newEmployee);
    	sscanf(lineOfText, "%s%s%d%s",   newEmployee->lastName,
    								     newEmployee->firstName,
    								     &newEmployee->id,
    								     newEmployee->class);
    	printf("%s\n", newEmployee->lastName);
    	
    	if(newEmployee != NULL)
    	{
    	  previousEmployee = NULL;
    	  currentEmployee = *firstEmployee;
    	  while((currentEmployee != NULL) && (strcmp(newEmployee->lastName, currentEmployee->lastName) > ZEROI))
    	  {
    	    previousEmployee = currentEmployee;
    		currentEmployee = currentEmployee->nextEmployee;
    	  }
    I"m getting a seg fault at the while loop, I have determined that this is due to the currentEmployee. setting currentEmployee = to *firstEmployee though I would think would solve this but I'm not getting anywhere.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Are you reading line outside the loop?
    You should check that the read opepration is succeded in the while condition

    sscanf(lineOfText, "%s%s%d%s", newEmployee->lastName
    if(newEmployee != NULL)
    you should check that newEmployee is NULL pointer, before you are trying to fill it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Segfault with additional variable?
    By misterFry in forum C++ Programming
    Replies: 11
    Last Post: 11-12-2008, 10:55 AM
  3. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  4. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  5. Segfault and Warning help
    By Uncle Rico in forum C Programming
    Replies: 1
    Last Post: 03-25-2005, 02:51 PM