Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all</p> <p>The following code is wierd</p> <pre><code>char getname(Record *names){ return names; } int getage(Record *ages){ return ages; } </code></pre> <p>I do not see any real need for the above function.</p> <p>even these lines</p> <pre><code>ptr-&gt;name=getname(p); ptr-&gt;age=getage(p); </code></pre> <p>you can relace them with</p> <pre><code>ptr-&gt;name=s; ptr-&gt;age=n; </code></pre> <p>The following function contains many errors and strange codes:</p> <pre><code>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); } </code></pre> <p>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:</p> <pre><code>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-&gt;name=s; ptr-&gt;age=n; /* link new object into the list */ ptr-&gt;next=*header; *headptr=ptr; } </code></pre> <p><strong>And in your main function:</strong></p> <pre><code>int main(int argc, char **argv) { int i= 0; Record *p, *headptr=NULL; for (int i=0; i &lt; 7; i++) { insert (&amp;headptr, names[i], ages[i]); /* do not dereference the pointer */ } for (int i=0; i &lt; 7; i++) { /* this will print from array*/ printf("From array The name is: %s, the age is:%i\n", p[i]-&gt;names, p[i]-&gt;ages); } for (p=headptr; p!=NULL; p=p-&gt;next) { /* this will print from linked list*/ printf("From linked list The name is: %s, the age is:%i\n", p-&gt;names, p-&gt;ages); } } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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