 <pre><code>cptr = malloc(256 * sizeof(char)); </code></pre> <p>Above line is wrong. Even if you compute number of bytes allocated above, it is 256 bytes. If size of pointer is 4 bytes on your machine, it would be enough for 64 pointers. Instead you need 256 pointers. Use</p> <pre><code>cptr = malloc(256 * sizeof(char *)); </code></pre>