title: Segmentation Fault when trying to split string
body: <p>for a larger program, I am trying to split a string after every n amount of characters. For example, ideally I should see something like this:</p>
  
  <pre><code>$ ./split
  str = this is a test that I am doing. No need to panic. This is just a test.
  
  =&gt;this &lt;=
  =&gt;is a &lt;=
  =&gt;test &lt;=
  =&gt;that &lt;=
  =&gt;I am &lt;=
  =&gt;doing&lt;=
  =&gt;. No &lt;=
  =&gt;need &lt;=
  =&gt;to pa&lt;=
  =&gt;nic. &lt;=
  =&gt;This &lt;=
  =&gt;is ju&lt;=
  =&gt;st a &lt;=
  =&gt;test.&lt;=
  </code></pre>
  
  <p>Instead I am seeing the following output:</p>
  
  <pre><code>$ ./split
  str = this is a test that I am doing. No need to panic. This is just a test.
  
  Segmentation fault: 11
  </code></pre>
  
  <p>I have stared at this code continuously, but cannot for the life of me find the cause of the segmentation fault. Any help would be greatly appreciated. Thank you! Here is the source code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  char **format(char *encoded_image, int encoded_image_size, int split) {
      int line_count = encoded_image_size / split + 1; /* One extra line for NULL */
      char **lines = malloc(sizeof(char *) * line_count);
  
      for(int iter = 0; iter &lt; line_count - 1; ++iter) {
          lines[iter] = malloc(split);
          lines[iter] = memcpy(lines[iter], encoded_image[split * iter], split);
          printf("%s\n", lines[iter]);
      }
  
      lines[line_count] = NULL;
      return lines;
  }
  
  int main() {
      char *str = strdup("this is a test that I am doing. No need to panic. This is just a test.");
      printf("str = %s\n\n", str);
      char **lines = format(str, strlen(str), 5);
  
      for(int iter = 0; lines[iter] != NULL; ++iter) {
          printf("=&gt;%s&lt;=\n", lines[iter]);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>(there might be a spelling mistake in the ideal output, because I had to do it by hand). (fixed a mistake in the for loop pointed out in the comments below)</p>