Well given those rules, how about this
Using varargs in the gcc pre-processor
Code:
#include <stdio.h>
#include <string.h>
// count the elements in an array
#define ASIZE(x) (sizeof(x)/sizeof(x[0]))
// initialise a local array of string pointers, and call join()
#define JOINER(dest,strings...) { \
char *temp[] = { "", ## strings }; \
join( temp, dest, ASIZE(temp) ); \
}
void join(char *strings[],char *dest,int num) {
int i;
for(i=0;i<num;i++)
dest=strcpy( dest,strings[i] )+strlen( strings[i] );
}
void test(void) {
char *string2="Var2",joined[80];
JOINER(joined);
printf( "'%s'\n", joined );
JOINER(joined,"Const1","Const2","Const3");
printf( "'%s'\n", joined );
JOINER(joined,"Const1",string2,"Const3");
printf( "'%s'\n", joined );
}
int main( ) {
test( );
return 0;
}
You can remove
"", ##
from the temp array in the macro if you can guarantee that you'll always pass parameters to the macro.
On the plus side, you no longer have to remember to change the count each time you change the number of parameters in a call.