Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8486723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:01:51+00:00 2026-06-10T21:01:51+00:00

Please let me know what’s wrong with this code, wherein I have added some

  • 0

Please let me know what’s wrong with this code, wherein I have added some nodes at the beginning and then displayed them,which are further tried to sort, but I’m not getting the sorted result…
thank you 🙂

#include <stdio.h>
#include <conio.h>
#include <malloc.h>


 struct node
{

int data;
struct node *link;
};

void append(struct node **q,int num)
{
 struct node *temp;
 temp=malloc(sizeof(struct node));
 temp->data=num;
 temp->link=*q;
 *q=temp;

}

void display(struct node *q)
{    struct node *temp;
 temp=q;
 printf("\n");
 while(q!=NULL)
{
    printf(" %d",q->data);
    q=q->link;
}
q=temp;
}
 void sort(struct node *q)
 {
 struct node *temp1, *temp2; int i,j,temp3;
temp1=q;
  temp2=q->link;
 for(i=0;i<6;i++)
{
    for(j=0;j<6-i;j++)
    {
        if(temp1->data>temp2->data)
        {
            temp3=temp1->data;
            temp1->data=temp2->data;
            temp2->data=temp3;
        }
        temp2=temp2->link;
    }
    temp1=temp1->link;
    temp2=temp1->link;
 }

}
void main()
{
struct node *p;
p=NULL;
append(&p,7);
append(&p,5);
append(&p,9);
append(&p,2);
append(&p,8);
display(p);
sort(p);
display(p);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:01:53+00:00Added an answer on June 10, 2026 at 9:01 pm

    I did not try to follow the entire logic, but your problem is you’re dereferencing NULL pointers:

    cristi:tmp diciu$ gdb ./a.out
    GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug  8 20:32:45 UTC 2011)
    [..]
    (gdb) r
    Starting program: /private/tmp/a.out 
    
    Program received signal EXC_BAD_ACCESS, Could not access memory.
    Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
    0x0000000100000d68 in sort (q=0x100100970) at test.c:44
    44          if(temp1->data > temp2->data)
    (gdb) p temp1
    $1 = (struct node *) 0x100100970
    (gdb) p temp2
    $2 = (struct node *) 0x0
    

    A hackish fix (I did not follow the code’s logic so I’m not sure this is correct) is to avoid dereferencing NULL pointers:

    void sort(struct node *q)
    {
         struct node *temp1, *temp2; int i,j,temp3;
         temp1=q;
         temp2=q->link;
         for(i=0;i<6;i++)
         {
             if(temp1==NULL || temp2==NULL)
                     continue;
             for(j=0;j<6-i;j++)
             {
                  if(temp2 == NULL)
                      continue;
                  if(temp1->data > temp2->data)
                  {
                      temp3=temp1->data;
    

    [..]

    The correct fix is to properly walk the list (you seem to assume the list has 6 elements when you can walk the list until you hit the NULL element since that is the end of the list).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please let me know if I'm coming at this block on a wrong angle.
Please let me know how to bug fix this code . I tried and
Please let me know how can I complete this switch code: switch ($urlcomecatid) {
Please let me know if this should be on Server Fault... I've got some
Please let me know if you have knowledge about some important sites that use
Can Someone please let me know how can I have this done in CSS?
Please let me know if you have any idea about it. Thanks EDIT What
Please let me know how to get the client IP address, I have tried
Please let me know if this is bad practice or in someway a bad
Please let me know if this has been asked before, I wasn't able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.