Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The chances are that (a) <code>response</code> is not initialized and (b) the <code>read()</code> function did not get to read a terminating null in the string. To demonstrate, use:</p> <pre><code>int rc = read(sockfd, response, sizeof(response)); printf("The response is: %.*\n", rc, response); </code></pre> <p>You should really check that <code>rc</code> is neither negative (a read failure) nor zero (EOF) before using it in the <code>printf()</code> statement, and you need to null terminate before passing it to <code>strtok()</code> et al, so maybe a better treatment is:</p> <pre><code>int rc = read(sockfd, response, sizeof(response)-1); if (rc &lt;= 0) ...error or EOF... response[rc] = '\0'; </code></pre> <hr> <blockquote> <p>I still get the error...</p> </blockquote> <p>You've marked the code where the error occurs at:</p> <pre><code>char *pch; pch = strtok(response, "\n"); printf("Part 1 -&gt; %s\n\n", pch); // ERROR </code></pre> <p>The most plausible reason for a core dump there is that <code>pch</code> contains a null pointer. So, to protect yourself, test the result from <code>strtok()</code>:</p> <pre><code>char *pch = strtok(response, "\n"); if (pch == 0) printf("strtok() failed\n"); else printf("Part 1 -&gt; %s\n\n", pch); </code></pre> <p>You should make sure that if <code>pch</code> is null, you don't continue using it.</p> <p>You didn't show the declaration of <code>rc</code>; if it was <code>unsigned char rc</code>, then the 255 value might indicate a -1 returned from the <code>read()</code> call.</p> <p>Also, the code I showed assumes that the definition of <code>response()</code> is visible as an array (either at file scope or function scope, not as a parameter to a function). When the array is a function parameter, <code>sizeof(response)</code> returns the same value as <code>sizeof(char *)</code>, which is not the size of the array in general.</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