Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are the errors I see:</p> <h2>Use of uninitialized pointer</h2> <p>In <code>substr</code> you declare <code>char *temp;</code> and then use it without initializing it to anything. This is not a compile-time error, but this program will almost certainly crash when you run it, since <code>temp</code> will effectively point to a random memory address. This is a case of <em>undefined behavior</em>, and C is chock full of it. Undefined behavior will come out of nowhere and eat your pets if you aren't careful.</p> <p>Consider <code>malloc()</code>ing some memory, or having your function receive a pointer to a buffer where it can write the portion of the string.</p> <h2>Use of function not yet declared</h2> <p>In C you must declare functions before they are used, or at least declare their prototype. Above <code>main()</code>'s declaration, add this line:</p> <pre><code>char * substr(char *string, int start, int length); </code></pre> <h2>No use of <code>const</code> where it makes sense</h2> <p>When assigning a string literal to a <code>char*</code>, that variable should be declared <code>const</code>. So change</p> <pre><code>char *string = "abcdefghi"; </code></pre> <p>to</p> <pre><code>const char *string = "abcdefghi"; </code></pre> <p>You will have to change your function prototype to</p> <pre><code>char * substr(const char *string, int start, int length) </code></pre> <p>which is what it should have been in the first place.</p> <p><strong>Added 2010-12-02:</strong></p> <h2><code>substr()</code> does not add terminating null character</h2> <p>The <code>substr()</code> function, while algorithmically correct in every other sense, does not add a terminating null character to the new string. This will cause <code>printf()</code> and every other string-using function (like <code>strlen()</code>, <code>strcpy()</code>, etc.) to run off the end of the string into unallocated heap memory, or stack memory (depending on how you resolve the "uninitialized pointer" issue).</p> <p>To fix this, add this line immediately after the <code>for</code> loop, and before the <code>return</code> statement:</p> <pre><code>temp[i] = '\0'; </code></pre> <p>Note that this should not be added <em>in</em> the for loop, as that would have the effect of creating a string with zero length.</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