Search code examples
cc-stringsstrcat

Pass %d to strcat() fucntion


I'm trying to pass something like this to a string but it does not work. I don't understand very well this function so I was hoping someone would explain to me what I'm doing wrong.

strcat(string,("%d;%d;%d;%d;%d;%s;%s;%s;%s;%s", x->n1, x->n2, x->n3, x->n4, x->n5, x->s1, x->s2, x->s3, x->s4, x->s5, x->s6));

Solution

  • It seems what you are trying to do is the following

    sprintf( string + strlen( string ), 
             "%d;%d;%d;%d;%d;%s;%s;%s;%s;%s", 
             x->n1, x->n2, x->n3, x->n4, x->n5, 
             x->s1, x->s2, x->s3, x->s4, x->s5 );
    

    Also instead of sprintf you can use snprintf that is safer.

    The character array string must contain a (possibly empty) string and be large enough to accommodate appended strings.