How to concatenate two strings (one of them is stored in a variable) in C using preprocessors?
For example how to do this?
#define CONCAT(x,y) x y
//ecmd->argv[0] is equal to "sometext"
myfunc(CONCAT("/", ecmd->argv[0]), ecmd->argv[0]); //error: expected ')' before 'ecmd'
You can't concatenate them using a macro like that. using the preprocessor, only raw strings (either string literals or names) can be concatenated.
You have to use strcat or some other technique to combine the strings. For example:
char * buf = malloc(strlen(ecmd->argv[0]) + 2);
buf[0] = '/'; buf[1] = '\0';
strcat(buf, ecmd->argv[0]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With