Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are at least several problems here.</p> <p>0: <code>temp[11]</code> is the twelfth element in a char array you've defined to be 11 elements long. This is a buffer overrun.</p> <p>1: <code>names[100][10]</code> should be <code>names[100][11]</code>, so that each element is large enough to store a value from <code>temp</code>.</p> <p>2: you're using strcpy(), then inserting a terminating character, presumably in case you copied more than 10 characters from strcpy(). In that case, you have a data overflow. You want to use strncpy(), and <em>then</em> terminate the string.</p> <pre><code>strcpy(temp, network[i].name); temp[11] = '\0'; </code></pre> <p>with</p> <pre><code>strncpy(temp, network[i].name, sizeof(temp) - 1); temp[sizeof(temp) - 1] = '\0'; </code></pre> <p>and replace</p> <pre><code> strcpy(names[nameCount], network[j].name); nameCount++; </code></pre> <p>with</p> <pre><code> strncpy(names[nameCount], network[j].name, sizeof(names[nameCount] - 1); names[nameCount][sizeof(nameCount) - 1] = '\0'; nameCount++; </code></pre> <p>3: the loop where you print the "and %s " list is dereferencing the array using the wrong variable. You're iterating using 'j', but pulling the 'i'th element out.</p> <p>4: as far as the newline goes, it's very likely the case that network[i].name (for any i) contains a newline character that you're copying in.</p> <p>5: if you have three things from the same location, you'll probably list them in a way you may not want.</p> <pre><code>1.1.1.1 chuck 1.1.2.2 larry 1.1.3.3 biff </code></pre> <p>will likely output (once the other bugs are fixed)</p> <pre><code>chuck and larry and biff are from the same location larry and biff are from the same location No matches for biff. </code></pre> <p>Fixing that problem is left as an exercise.</p>
 

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