title: C copy char * to char[]
body: <p>Hello I am trying to copy a char * pointer to a char [] array.</p>
  
  <p>this is my code so far</p>
  
  <pre><code>char * string_add(char * base, char * toAdd)
  {
      char * string=malloc(strlen(base)+streln(toAdd)+1);
      sprintf(string,"%s%s",base,toAdd);
      char returnString[strlen(string)+1]; 
      // here comes my problem:
      memcpy(returnString,string,strlen(string)+1);
      // want to add free(string) here
      return returnString;
  }
  </code></pre>
  
  <p>I want to use such a function to save code. I don't want to look after every allocated memory. I also tried</p>
  
  <pre><code>memcpy(&amp;returnString,string,strlen(string)+1);
  </code></pre>
  
  <p>and some variants with strcpy and strncpy. But following problem persists:</p>
  
  <p>if I call the function twice like:</p>
  
  <pre><code>int main(int argc, char * argv[])
  {
      char * str1=string_add(argv[1],"-test1"); 
      char * str2=string_add(argv[1],"-test2"); 
      printf("%s, %s", str1,str2);
  }
  </code></pre>
  
  <p>the output is like:</p>
  
  <pre><code>abc-test2, abc-test2
  </code></pre>
  
  <p>How can I realize this? </p>
  
  <p>Thank you in advance!</p>