How to concat multiple strings in C?
I have a function for concat two strings:
char* concat(char *s1, char *s2)
{
char *r, *t;
int d1 = -1, d2 = -1;
while (s1[++d1]);
while (s2[++d2]);
t = r = (char *)calloc(d1 + d2 + 1, sizeof(char));
while (*t++ = *s1++);
t--;
while (*t++ = *s2++);
return r;
}
Is there a way to use this function with two strings to implement with multiple strings?
For example, prototype is
char * concat(char **array, int n);
Also, is this dynamic allocation right:
char* concat(char** array, int n)
{
char *r; int i;
r=(char *)calloc(n*MAX+1, sizeof(char));
array=(char **)malloc(n * sizeof(char *));
for(i=0;i<n;i++)
{
array[i]=(char *)malloc(MAX+1);
}
//concatenation//
free(r);
free(array[i]);
free(array);
}
Thanks for replies.