Note that there are some explanatory texts on larger screens.

plurals
  1. POSearching through a linked list in C
    primarykey
    data
    text
    <p>I have been working on a function <code>searchn()</code> which takes in a linked list <code>list</code> and a string <code>lname</code>.</p> <p>It compiles perfectly fine, but I get a segmentation fault (core dumped) when I try running the function. I ran it through Valgrind and it tells me that I'm using <code>strcpy</code> and <code>strcmp</code> incorrectly. My struct for <code>player</code> has also been included for reference. Can anybody see what I'm doing wrong? Sorry, I'm not the most proficient in coding. </p> <p>Any help would be great. Thanks. </p> <pre><code>struct player { char* fname; char* lname; char pos; int val; int rank; struct player* next; }; void searchn(struct player* list, char* lname){ while (list!=NULL &amp;&amp; strcmp(list-&gt;lname, lname) != 0){ list = list-&gt;next; } if (list != NULL &amp;&amp; strcmp(list-&gt;lname, lname)==0) { printf("%s found! \n", lname); printf("%s \n", list-&gt;lname); printf("%s \n", list-&gt;fname); printf("%c \n", list-&gt;pos); printf("%d \n", list-&gt;val); printf("\n"); } } </code></pre> <p>The following is how the method that populates the linked list. </p> <pre><code>void addp (struct player* newnode, struct player* list){ struct player* templist1; // if the list is non empty. if (list !=NULL){ if(newnode-&gt;pos == GOALKEEPER){ //insert if G. while (list-&gt;next != NULL &amp;&amp; (list-&gt;next)-&gt;rank &lt; 1){ list = list-&gt;next; } templist1 = list-&gt;next; list-&gt;next = newnode; newnode-&gt;next = templist1; } if(newnode-&gt;pos == DEFENDER){// after G bef M. // iterate through templist. while (list-&gt;next != NULL &amp;&amp; (list-&gt;next)-&gt;rank &lt; 2) { // go to end of G. // when the list isn't empty next node rank is less than one, keep going list = list -&gt; next; } // when finally rank == or &gt; 1, then add newnode. templist1 = list-&gt;next; list-&gt;next = newnode; newnode-&gt;next = templist1; } if(newnode-&gt;pos == MIDFIELDER){ //after G and M but before S while (list-&gt;next != NULL &amp;&amp; (list-&gt;next)-&gt;rank &lt; 3) { list = list -&gt; next; } // when stopped, then add newnode. templist1 = list-&gt;next; list-&gt;next = newnode; newnode-&gt;next = templist1; } if(newnode-&gt;pos == STRIKER){ // at the end. while (list-&gt;next != NULL &amp;&amp; (list-&gt;next)-&gt;rank &lt; 4){ list = list -&gt; next; } templist1 = list-&gt;next; list-&gt;next = newnode; newnode-&gt;next = templist1; } printf("player added"); } } </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.
 

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