Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/a/8662935/377270">Dan definitely found one error, the identical storage</a>. But I think there are more bugs here:</p> <pre><code>while((fgets(line, 65, fpIn)) != NULL) { </code></pre> <p>Why only <code>65</code>? You've got <code>MAXLEN</code> space to work with, you might as well let your input be a bit longer.</p> <pre><code> j = strlen(line); if (j &gt; 0 &amp;&amp; (line[j-1] == '\n')) { line[j-1] = '\0'; } if (j &gt; 8) { lineptr[nlines++] = line; } } </code></pre> <p>Why exactly <code>j &gt; 8</code>? Are you supposed to be throwing away short lines? Don't forget to <em>deallocate</em> the memory for the line in this case, once you've moved to the dynamic allocation that Dan suggests.</p> <p><strong>Update</strong></p> <p><a href="https://stackoverflow.com/questions/8662917/c-array-of-char-arrays/8662952#comment-10765767">ott recommends <code>strdup(3)</code></a> -- this would be easy to fit into your existing system:</p> <pre><code>while((fgets(line, 65, fpIn)) != NULL) { j = strlen(line); if (j &gt; 0 &amp;&amp; (line[j-1] == '\n')) { line[j-1] = '\0'; } if (j &gt; 8) { lineptr[nlines++] = strdup(line); } } </code></pre> <p>Dan recommended <code>calloc(3)</code>, that would be only slightly more work:</p> <pre><code>line = calloc(MAXLINE, sizeof char); while((fgets(line, 65, fpIn)) != NULL) { j = strlen(line); if (j &gt; 0 &amp;&amp; (line[j-1] == '\n')) { line[j-1] = '\0'; } if (j &gt; 8) { lineptr[nlines++] = line; line = calloc(MAXLINE, sizeof char); } } </code></pre> <p>Of course, both these approaches will blow up if the memory allocation fails -- checking error returns from memory allocation is always a good idea. And there's something distinctly unbeautiful about the second mechanism.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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