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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:59:16+00:00 2026-06-14T21:59:16+00:00

I would like to insert the struct at the end of the list, and

  • 0

I would like to insert the struct at the end of the list, and I can not get the code compiled.
Here are some of the pseudo code I had for the insert() function. They rest of the code should remain the same.

 (check it succeeded)
 set the data for the new person
 if the current list is empty (i.e. NULL)
   do the same as insert_start i.e.
   set the new person's "next" link to point to the current list (i.e. NULL)
   return the (start of the) new list i.e. a pointer to the new person
 otherwise
   use a loop to find the last item in the list
     (i.e. the one which has a "next" link of NULL)
   set the "next" link of this item to point to the new person
     so the new person becomes the last item in the list
     (i.e. the new person should have a "next" link of NULL)
       return the (start of the) list

Compile errors:

error: 'true' undeclared (first use in this function)
insert_end.c:47:13: note: each undeclared identifier is reported only once for each function it    appears in
insert_end.c:57:3: warning: 'return' with a value, in function returning void
make: *** [insert_end] Error 1




#include <stdio.h>
#include <stdlib.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;

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

  /* allocate heap space for a record */
  Record *ptr =(Record*) malloc(sizeof(Record));

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

  ptr->name=s;
  ptr->age=n;
  ptr->next = NULL;

  if(p->next==NULL)
  {
     p->next= ptr;

  }else{
      Record *current = p;
      while(true){

    if(current->next ==NULL)
{
  current->next = ptr;
  break;
}
current= current->next;
  }
  }
  return 0;

}  

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

      /* declare the people array here */
  Record *p;
  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);
  }


}
  • 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-14T21:59:16+00:00Added an answer on June 14, 2026 at 9:59 pm

    ‘true’ and ‘false’ need to be defined in c. Otherwise use 1 and 0 instead.
    return 0 will not work as the function is defined as void. either remove ‘void from the declaration or use ‘return;’

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

Sidebar

Related Questions

In a part of my website I would like to insert some images and
I have a JPA project and I would like to insert some initial data
I would like to get some help with the following question. I have a
Help Perlers! Does anyone know a simple insert code here approach would bring code
I would like to insert the current Subversion revision number (as reported by svnversion
I would like to insert dates in an emacs buffer like I do in
I would like to insert notes on the fixes to be done in specific
I would like to insert the output of multiple calculations into my text document:
I would like to insert a new DataGridViewRow into my DataGridView at a specific
I have a little problem where I would like to insert a svn diff

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.