title: Strings with malloc in C
body: <p>I am writing a very simple program to copy a string using <em>malloc</em>. </p>
  
  <pre><code>#include &lt;stdio.h&gt; 
  #include &lt;stdlib.h&gt; 
  
  char * copyStr(char s[])
      {
        int len = strlen(s); //find length of s
        char * copy; 
        copy = (char *)malloc(len); //dynamically allocate memory 
        int i; 
        for(i=0;i&lt;len;i++) 
         {
           copy[i]=s[i];  //copy characters               
         }
         return copy; //return address
      }
  
  int main(int argc, char ** argv)
      {
       char * str;
       str = "music is my aeroplane";
       char * res;
       res = copyStr(str);
       printf("The copied string is : %s",res);
       getch();
      }
  </code></pre>
  
  <p>The desired output is:</p>
  
  <pre><code>The copied string is : music is my aeroplane
  </code></pre>
  
  <p>The current output is: </p>
  
  <pre><code>The copied string is : music is my aeroplaneOMEeJ8«≤╝
  </code></pre>
  
  <p>Any advice is appreciated. </p>