Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are several things wrong with this. </p> <p>First, your constant array is not declared correctly.</p> <pre><code>char tracks[][5] = { "one", "two", "three", "four", "five", "six", }; </code></pre> <p>This says "declare an array of arbitrary length of <code>char[5]</code>. Stare at that array content <em>very</em> closely, and consider that the length of those strings is actually its character count <strong>plus one for the zero-terminator</strong>. Anything pop out at you? Perhaps the word <code>"three"</code> ? That would be 5+1, or <strong>six</strong> chars wide, not five.</p> <p>Try this:</p> <pre><code>const char *tracks[] = { "one", "two", "three", "four", "five", "six" }; </code></pre> <p>And also modify your for loop like so:</p> <pre><code>for (i = 0; i &lt; sizeof(tracks)/sizeof(tracks[0]); ++i) { if (strstr(tracks[i], search_for)) printf("track[%d]: %s\n", i, tracks[i]); } </code></pre> <p>Note: I pulled the redundant "Nothing found" from the loop for my own sanity.</p> <p>Finally, the string fetched will likely have an end-of-line (<code>'\n'</code>) tacked on to the end, and you should check and null it if so. I would significantly lengthen the size of your input buffer, then trim the endl out if it is there:</p> <pre><code>int main() { char search_for[64] = {0}; printf("enter your word: "); if (fgets(search_for, sizeof(search_for), stdin)) { size_t len = strlen(search_for); if (len &amp;&amp; search_for[len-1] == '\n') search_for[len-1] = 0; track_search(search_for); } else { perror("fgets failed."); return EXIT_FAILURE; } return EXIT_SUCCESS; } </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