Note that there are some explanatory texts on larger screens.

plurals
  1. POWord occurrences and line numbers
    text
    copied!<p>I'm making a program that generates a text file that contains the occurrences of words and the line number of each occurrence of another text file. I'm using an AVL tree struct that contains the word and a linked list struct that contains one node for each line number. Here are the struct definitions:</p> <pre><code>struct llnode { struct llnode *next; int num; }; struct node { char *word; struct llnode *head; struct node *left; struct node *right; int height; }; </code></pre> <p>I get a segmentation fault when I try to print to the text file using the below functions.</p> <pre><code>void listprint(struct llnode *p) { if(p-&gt;next == NULL) { printf("%d", p-&gt;num); } else { printf("%d, ", p-&gt;num); listprint(p-&gt;next); } } void treeprint(struct node *p) { if(p != NULL) { treeprint(p-&gt;left); printf("%s: ", p-&gt;word); listprint(p-&gt;head); treeprint(p-&gt;right); } } </code></pre> <p>Specifically the problem is this line</p> <pre><code>if(p-&gt;next == null) { </code></pre> <p>gdb gives me</p> <pre><code>Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000 </code></pre> <p>Thanks for your help.</p> <p>Edit:</p> <pre><code>void listinsert(struct llnode *p) { struct llnode *prev = p; while(p != NULL) { prev = p; p = p-&gt;next; } p = lalloc(); p-&gt;num = line; p-&gt;next = NULL; prev-&gt;next = p; struct node *addtree(struct node *p, char *w) { int cond; if(p == NULL) { p = talloc(); p-&gt;head = NULL; p-&gt;word = mystrdup(w); p-&gt;head = listinsert(p-&gt;head); p-&gt;left = p-&gt;right = NULL; } else if((cond = strcmp(w, p-&gt;word)) == 0) { listinsert(p-&gt;head); } else if(cond &lt; 0) { p-&gt;left = addtree(p-&gt;left, w); if(height(p-&gt;left)-height(p-&gt;right) == 2) { if(strcmp(w, p-&gt;left-&gt;word) &lt; 0) { p = singleleft(p); } else { p = doubleleft(p); } } } else { p-&gt;right = addtree(p-&gt;right, w); if(height(p-&gt;right)-height(p-&gt;left) == 2) { if(strcmp(w, p-&gt;right-&gt;word) &gt; 0) { p = singleright(p); } else { p = singleleft(p); } } } return p; int getword(char *word, int lim) { int c; char *w = word; while(isspace(c = getch())); if(c == '\n') { line++; } if(c != EOF) { *w++ = c; } if(!isalpha(c)) { *w = '\0'; return c; } for( ; --lim &gt; 0; w++) { if(!isalnum(*w = getch())) { ungetch(*w); break; } } *w = '\0'; return word[0]; </code></pre>
 

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