Thread: Help With Segmentation Error

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Oh, also in the sort, you don't need to move the strings around, just the pointers. That removes the need to malloc more memory:

    Code:
       for (i = 0; i < count; i++) {
          for (k = i + 1; k < count; k++) {
             if(bk[i].LastName == NULL ||bk[k].LastName == NULL) // Avoid segfaults with deleted items
                continue;
    
    
             //My if statement to manually sort user data in alphabetical order by last name
             if (strcmp(bk[i].LastName,bk[k].LastName) > 0) {
                char *temp;
    
    
                temp =  bk[k].FirstName;
                bk[k].FirstName = bk[i].FirstName;
                bk[i].FirstName = temp;
    
    
                temp =  bk[k].LastName;
                bk[k].LastName = bk[i].LastName;
                bk[i].LastName = temp;
    
    
                temp =  bk[k].number;
                bk[k].number = bk[i].number;
                bk[i].number = temp;
             }
          }
       }
    Also, you are only sorting by LastName, your comment indicates you know that
    Last edited by hamster_nz; 10-23-2020 at 10:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation error!
    By oceans in forum C Programming
    Replies: 11
    Last Post: 07-31-2014, 06:04 AM
  2. segmentation error
    By MASX in forum C Programming
    Replies: 6
    Last Post: 03-12-2011, 07:52 AM
  3. Segmentation Error
    By Native in forum C Programming
    Replies: 2
    Last Post: 10-26-2010, 08:41 AM
  4. segmentation error
    By callkalpa in forum C Programming
    Replies: 2
    Last Post: 12-13-2009, 03:27 AM
  5. Segmentation Error / Bus Error in ANSI
    By drag0n69 in forum C Programming
    Replies: 10
    Last Post: 02-05-2008, 09:45 AM

Tags for this Thread