title: Modify char array passed to a function, C
body: <p>I would like to concat 2 strings inside a function. However, I would like the function to modify also the destination string (char array). </p>
  
  <p>So far I got this code below, but it shows 'segmentation fault' and I do not know how can I fix this. Thanks.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  char *my_strcat(char *dest, const char * src) {
      char *tab = (char*) malloc(sizeof(char) * (strlen(*dest) + strlen(src) + 1));
      if(NULL == tab)
          return NULL;
      strcpy(tab, dest);
      strcpy(tab, src);
      free(dest);
      dest = (char*)malloc(sizeof(char) * (strlen(tab)+1));
      strcpy(dest, tab);
      return tab; }
  
  int main(int argc, char **argv) {
      char *dst = NULL, *src = NULL, *r = NULL;
      int i;
      src = malloc(sizeof(char) * 100);
      strcpy(src, "fifty");
  
      dst = malloc (sizeof (char *) * 100);
      strcpy(src, "four");
  
      r = my_strcat(dst, src);
      printf("%s\n", r);
      printf("%s\n", dst);
  
      free(r);
      free(dst);
      free(src);
  
      return 0; }
  </code></pre>