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

The Archive Base Latest Questions

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

In this program, I want to put the element in the list rather than

  • 0

In this program, I want to put the element in the list rather than an array. And lastly print out the list.

eg.
simon 22
suzie 24
…

However, I dont really understand how to manipulate the list, and how I can build the heap and retrieve it. I did some research about how to do it. And this is what I come up.
And some of the error comes out and I have no idea how to fix.

error: 'ptr' undeclared (first use in this function)
arrays.c:37:5: note: each undeclared identifier is reported only once for each function     it appears in
arrays.c: In function 'main':
arrays.c:62:9: error: expected identifier or '(' before '=' token
arrays.c:69:5: warning: passing argument 1 of 'insert' from incompatible pointer type
arrays.c:28:13: note: expected 'struct Record *' but argument is of type 'struct Record **'

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


/* these arrays are just used to give the parameters to 'insert',
   to create the 'people' array */
char *names[7]= {"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 *names;
    int ages;
    struct Record *next;
}  Record;

char getname(Record *names){
    return names;
}

int getage(Record *ages){
    return ages;  
}

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

//p[(*)] = malloc(sizeof(person));

/*static int nextfreeplace = 0;*/
    Record *headptr = NULL;

    while(!reached_eof(p)){

/* allocate heap space for a 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 = getname(p);
        ptr->age = getage(p);

        /* link new object into the list */
        ptr->next = headptr;
        headptr = ptr;

    }
}       


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

    /* declare nextinsert */
    int  = 0;            

    /* 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");


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


    /* 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[i]);
    }
}
  • 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:40:18+00:00Added an answer on June 14, 2026 at 10:40 pm

    First of all

    The following code is wierd

    char getname(Record *names){
      return names;
    }
    
    int getage(Record *ages){
      return ages;  
    }
    

    I do not see any real need for the above function.

    even these lines

    ptr->name=getname(p);
    ptr->age=getage(p);
    

    you can relace them with

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

    The following function contains many errors and strange codes:

    static void insert (Record *p, char *s, int n) {
    
    //p[(*)] = malloc(sizeof(person));
    
    /*static int nextfreeplace = 0;*/
       Record *headptr = NULL;
    
       while(!reached_eof(p)){
    /* allocate heap space for a 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);      
    }
    

    Why you are using while loop. and you missed the definition of ptr pointer and you have note communicate the neaw header at the end of the function. here after how you can fix it:

    static void insert (Record **header, char *s, int n) {
    
       Record *ptr;
       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;
    
        /* link new object into the list */
        ptr->next=*header;
        *headptr=ptr;
    }
    

    And in your main function:

    int main(int argc, char **argv) {
    
    
       int  i= 0;
      Record *p, *headptr=NULL;
    
    for (int i=0; i < 7; i++) {
    insert (&headptr, names[i], ages[i]);
    /* do not dereference the pointer */
      }
    
     for (int i=0; i < 7; i++) { /* this will print from array*/
        printf("From array  The name is: %s, the age is:%i\n", p[i]->names, p[i]->ages);
    }
    
    for (p=headptr; p!=NULL; p=p->next) {  /* this will print from linked list*/
        printf("From linked list The name is: %s, the age is:%i\n", p->names, p->ages);
    }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I want to make my program print out the date: 1/1/2009 But this
I want to write this program to find a keyword in a list. If
In this program, I would like to print out information about people: the names
this program is for class to display the database in Listview.i want to add
I have written php program and uploaded on server. I want run this program
I don't know how to code this but i want program to skip or
this is something that i don't want to program, but i was looking for
I have this statement in my c program and I want to optimize. By
This is the last part of the program I am working on. I want
I want to know how to check whether is program running and run this

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.