title: Simple C program. String returning from function causes Error
body: <p>The following is the exercise 10.4 of the book Programming in C, by Stephen Kochan. It says that I should create a function, that derives a part from an input string and returns that part back to <code>main()</code> (as a string, not a pointer) and displays it.
  My code is below.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  char subString (const char source[], int start, int count, char result[count + 1] ){ //result will be number of characters (count) + 1 (because of null)
  int i, j, end = start + count;
  
  // the part excluded must start from i = start and "count" number of characters must be derived and then put on result
  for( i = start, j = 0; i &lt; end; ++i, ++j)
      result[j] = source[i];
  
  result[j] = '\0';
  
  return result[count + 1];
  }
  
  int main (void){
  char result[20] = {0};
  
  const char text1[] = "character";
  
  result[20] = subString( text1, 4, 3, result );
  printf("From \"%s\" this part is being excluded-&gt; \"%s\"\n", text1, result);
  
  return 0;
  }
  </code></pre>
  
  <p>And the output is</p>
  
  <pre><code>From "character" this part is being excluded-&gt; "act"
  
  Process returned 0 (0x0)   execution time : 0.332 s
  Press any key to continue.
  </code></pre>
  
  <p>Notice that the code above runs perfectly well - no warnings.</p>
  
  <p>What i can't understand is when i replace the two lines below</p>
  
  <pre><code>result[20] = subString( text1, 4, 3, result );
  printf("From \"%s\" this part is being excluded-&gt; \"%s\"\n", text1, result);
  </code></pre>
  
  <p>with this line</p>
  
  <pre><code>printf("From \"%s\" this part is being excluded-&gt; \"%s\"\n", text1, subString( text1, 4, 3, result ) );
  </code></pre>
  
  <p>i get the output:</p>
  
  <pre><code>From "character" this part is being excluded-&gt; "(null)"
  
  Process returned 0 (0x0)   execution time : 0.332 s
  Press any key to continue.
  </code></pre>
  
  <p>Why is that? How can i make it work using that one line instead? 
  Also, I'm a little confused with functions returning strings/arrays. They tend to lead me on errors, so if someone could provide me a couple of suggestions, that i should always keep in mind when working with them, that would help me very much. Thank you in advance.</p>