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 8890169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:26:20+00:00 2026-06-14T22:26:20+00:00

The code is compiled, however, there are logical errors in my code. I want

  • 0

The code is compiled, however, there are logical errors in my code. I want to compare the string in the array and then list them in order in the list.I cant not figure out, how I can compare the list items without using index, and how can i compare the current name with the next name. Any one can help?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* these arrays are just used to give the parameters to 'insert',
   to create the 'people' array */
char names[][10]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
      "Harriet"};
int ages[7]= {22, 24, 106, 6, 18, 32, 24};


/* declare your struct for a person here */
typedef struct Record{
    char *name;
    int age;
    struct Record *next;
}   Record;

//set the head pointer at the start of the list
Record *headptr = NULL;

int compare_people( Record *a, Record *b)
{
     return strcmp((*(Record *)a).name, (*(Record *)b).name);
}

static void insert (Record *p, char *s, int n) {

    /* create a new space for the new person */
    Record *ptr = ( Record *) malloc(sizeof(Record));

    /* check if it is succeeded  */ 
    if( ptr == NULL){  
        abort();
        printf("memory allocation fail"); 
        exit(1);  
    }else{
        printf("memory allocation to person  - %s - \n", s);      
    }

    //set the data for the new person
    ptr->name=s;
    ptr->age=n;
    ptr->next= NULL;

    //ptr= NULL; 
    //printf("%i", p->age);


    /*  do not compare when the list is empty*/
    if(headptr==NULL)
    {
        ptr->next=headptr;
        headptr=ptr;
        printf("ok1\n");

    }else{
        Record *tail = headptr;

        /* go through all the list */
        while(tail->next!=NULL)
        {     
            if(compare_people(ptr->name,tail->name)== 1){
            tail = tail->next;
        }else{
            tail->next=headptr;
            }
        }//while

        //tail->next=ptr;
    }  
}  

int main( int argc, char **argv) {

    /* declare the people array here */
    Record *p=headptr;
    headptr = NULL;

    //insert the members and age into the unusage array. 
    for (int i=0; i < 7; i++) {
         insert (p,names[i], ages[i]);
         /* do not dereference the pointer */
    }

    /* print out a line before printing the names and ages */
    printf("\n");

    //set the pointer at the start of the list 
    p = headptr;

    /* print the people array here*/
    for ( int i=0; i < 7; i++, p = p->next ) {
        printf("The name is: %s, the age is:%i\n", p->name, p->age);
    }


    /* This is the third loop for call free to release the memory allocated by malloc */
    /* the free()function deallocate the space pointed by ptr. */
    for( int i=0; i<7; i++){
        free(p->next);
    } 
}
  • 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-14T22:26:21+00:00Added an answer on June 14, 2026 at 10:26 pm

    Your code contains many error . I can not go over all errors of your code and comments on. I tried to fix your code .

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char names[][10]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
          "Harriet"};
    int ages[7]= {22, 24, 106, 6, 18, 32, 24};
    
    
    /* declare your struct for a person here */
    typedef struct Record{
      char *name;
      int age;
      struct Record *next;
    }  Record;
    
    //set the head pointer at the start of the list
    Record *headptr = NULL;
    
    int compare_people(char *a, char *b)
    {
    
      return strcmp(a, b);
    }
    
    void insert (char *s, int n) {
         Record *t, *pnew, *prv;
         int i;
         prv=NULL;
         pnew=(Record *)malloc(sizeof(struct Record));
         if(pnew == NULL){  
            abort();
            printf("memory allocation fail"); 
            exit(1);  
        }else{
            printf("memory allocation to person  - %s - \n", s);      
        }
         pnew->name = s;
         pnew->age = n;
         pnew->next = NULL;
         if (headptr==NULL)
         {
            headptr = pnew;
            return;
         }
         for (t=headptr;t!=NULL;t=t->next) { // look for the right place to insert in order to get a tri list
             if (compare_people(s,t->name)<0) {        
                pnew->next=t;
                if (prv!=NULL)
                   prv->next = pnew;
                else
                   headptr=pnew;
                return;
             }
             prv=t;
         }
         prv->next=pnew;
         return;       
    }
    
    int main(int argc, char **argv) {
    
      Record *p, *q;
      int i;
    
      for (i=0; i < 7; i++) {
         insert (names[i], ages[i]);
      }
    
       printf("\n");
    
      for (p = headptr; p!=NULL; p = p->next) {
        printf("The name is: %s, the age is:%i\n", p->name, p->age);
      }
    
    
      /* To free your linked list: */
      p = headptr;
      while (p!=NULL){
        q = p;
        p = p->next;
        free(q);
      }
    }
    

    The output of the execution of the above code:

    linux$ ./test
    memory allocation to person  - Simon - 
    memory allocation to person  - Suzie - 
    memory allocation to person  - Alfred - 
    memory allocation to person  - Chip - 
    memory allocation to person  - John - 
    memory allocation to person  - Tim - 
    memory allocation to person  - Harriet - 
    
    The name is: Alfred, the age is:106
    The name is: Chip, the age is:6
    The name is: Harriet, the age is:24
    The name is: John, the age is:18
    The name is: Simon, the age is:22
    The name is: Suzie, the age is:24
    The name is: Tim, the age is:32
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python is compiled into an intermediate bytecode(pyc) and then executed. So, there is a
I'm not too good with C++, however; my code compiled, but the function crashes
I am trying to compile a code that uses Boost's zlib wrappers. However, no
if I compile (under G++) and run the following code it prints Foo::Foo(int). However
I have a WPF project which compiles just fine. However, when I enable code
This code compiled properly on Visual Studio 2010 on Windows, but I'm getting this
I have the following code compiled by gcc: #include <iostream> using namespace std; class
the following piece of C++ code compiled two years ago in a suse 10.1
I have a piece of C++ code (compiled with g++ under a GNU/Linux environment)
I have been given some matlab code compiled using the .net compiler. I can

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.