I have programmed a self-made concat function:
char * concat (char * str1, char * str2) {
for (int i=0; i<BUFSIZ; i++) {
if (str1[i]=='\0') {
for (int j=i; j<BUFSIZ; j++) {
if (str2[j-i]=='\0') return str1;
else str1[j]=str2[j-i];
}
}
}
}
Now if I want to concat more than 2 strings, i.e. buf temp1 temp2,
I have to use something like that:
strcpy(buf, concat(concat(buf,temp1),temp2));
Please tell me, is there a simple way to modify my function so it would accept many arguments?
The feature you’re looking for is
varargs. This allows you to write a C function which accepts a variable number of arguments. It’s how functions likeprintfare implementedNow you can call concat with a variable number of arguments