Hi - please help. I have two strings and a number which I need to concatenate together and I'm confused to what is the best approach. Here is my code:
i get a seg fault by the way. can anyone see what i am trying to do and advise me please?
Code:
char *s1 = "iamafilename_";
char *s2 = ".tif";
int i;
for( i = 0; i < 10; i++ )
{
char itoaBuff[100];
int num_len = sprintf(itoaBuff, "%s", i);
char *f = (char *)malloc( (strlen(s1) + strlen(s2) + num_len) * sizeof(char) + 1 );
strcat( f, s1 );
strcat( f, itoaBuff );
strcat( f, s2 );
printf("the three strings concatenated is: %s\n\n", f);
free( f );
}
i get a seg fault by the way. can anyone see what i am trying to do and advise me please?
Comment