Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked List in C (apparently malloc doesn't want to be my friend)
    primarykey
    data
    text
    <p>I was so pleased with myself getting this to work last night without any errors or warnings on my first try too! But, of course, I changed a bunch of stuffed and screwed it up... When I tried to gdb it, the list from this.ytxt seemed load to memory just fine. I think the problem was writing it. Now. it works again but only writes the first line of the file. I commented out whole functions and printf'd test marks and still couldn't figure it out. the idea is to read a variable number of lines from a file and print them in pairs. (Actually it was meant to be like study flashcards but I never got around to that part)</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; struct member{ //This emulates a dictionary char key[20]; //and links each word+definition pair to the next pair. char def[20]; struct member *ptr; }; struct member *root; struct member *curr; FILE *f; int fill_list(){ //Fill each member struct with next two lines of curr=root; char this[20]; //FILE *f while(EOF){ if(!fgets(this,20,f)) break; strcpy(curr-&gt;key,this); if(!fgets(this,20,f)) break; strcpy(curr-&gt;def,this); curr=curr-&gt;ptr; curr=malloc(sizeof(struct member)); curr-&gt;ptr=0; } return 0; } void free_all(){ curr=NULL; while(curr!=root){ curr=root; while(curr-&gt;ptr)curr=curr-&gt;ptr; free(curr); } } int terminate(int i){ //The terminate function closes file and free_all(); //frees malloc'd memory, then returns main func's fclose(f); //return value. return i; } int main(){ f=fopen("this.txt","r"); if(!f)return -1; root=malloc(sizeof(struct member)); root-&gt;ptr=NULL; fill_list(); curr=root; if ( curr != 0 ) { while ( curr != 0 ) { printf( "%s", curr-&gt;key ); curr = curr-&gt;ptr; } } free_all(); return terminate(0); } </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