Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As everyone else has stated, use <code>strlen</code> not <code>sizeof</code>. The reason this is happening though, is a fundamental concept of <code>C</code> that is different from <code>Java</code>. </p> <p><code>Java</code> does not give you access to <a href="https://en.wikipedia.org/wiki/Pointer_%28computer_programming%29" rel="nofollow">pointers</a>. Not only does <code>C</code> have pointers, but <em>they are fundamental to the design of the language</em>. If you don't understand and use pointers properly in <code>C</code> then things won't make sense, and you will have quite a bit of trouble.</p> <p>So, in this case, <code>sizeof</code> is returning the size of the <code>char *</code> pointer, which is (usually) 4 or 8 bytes. What you want is the length of the data structure "at the other end" of the pointer. This is what <code>strlen</code> encapsulates for you.</p> <p>If you didn't have <code>strlen</code>, you would need to dereference the pointer, then walk the string until you find the null byte marking the end.</p> <pre><code>i = 1; while(*guess++) { i++ } </code></pre> <p>Afterwards, <code>i</code> will hold the length of your string.</p> <p><strong>Update:</strong></p> <p>Your code is fine, except for one minor detail. The <a href="http://en.cppreference.com/w/c/io/fgets" rel="nofollow">docs for fgets</a> note that it will keep the trailing newline char.</p> <p>To fix this, add the following code in between the <code>fgets</code> and <code>strncmp</code> sections:</p> <pre><code>if ( guess[guessLen-1] == '\n' ) { guess[guessLen-1] = '\0'; } </code></pre> <p>That way the trailing newline, if any, gets removed and you are no longer off by one.</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. 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.
 

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