title: C - create a string "from" struct parameter
body: <p>Have a </p>
  
  <pre><code>typedef struct person {
      char name[20]
      char surname[20]
  } person_t;
  </code></pre>
  
  <p>I need to create a string like <code>XXXXXX:YYYYYY</code> with the function like
  <code>char* personToString(person_t *p)</code>. I tried to make it:</p>
  
  <pre><code>char* personToString(person_t* p) {
  
    int n1,n2;
    n1=strlen(p-&gt;name);
    n2=strlen(p-&gt;surname);
    char *p = (char*) malloc((n1+n2+2)*sizeof(char));
    strcat(p,puser-&gt;name);
    strcat(p,":");
    strcat(p,puser-&gt;surname);
  
    return p;
  }
  </code></pre>
  
  <p>This give me a reasonable output but I have some errors testing with valgrind! I also think that there is a way more classy to write the function!</p>