Note that there are some explanatory texts on larger screens.

plurals
  1. POPut array items in a list and print out the list items in C
    primarykey
    data
    text
    <p>In this program, I want to put the element in the list rather than an array. And lastly print out the list. </p> <p>eg. simon 22 suzie 24<br> ...</p> <p>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. </p> <pre><code>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 &lt;stdio.h&gt; #include &lt;stdlib.h&gt; /* 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-&gt;name = getname(p); ptr-&gt;age = getage(p); /* link new object into the list */ ptr-&gt;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 &lt; 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 &lt; 7; i++) { printf("The name is: %s, the age is:%i\n", p[i]-&gt;names, p[i]-&gt;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&lt;7;i++){ free(p[i]); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload