segFault

This is a discussion on segFault within the C Programming forums, part of the General Programming Boards category; Code: void hire(FILE **infile, char lineOfText[], struct employee **firstEmployee) { struct employee *newEmployee = NULL; struct employee *previousEmployee = NULL; ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 09: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, 01:51 PM

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