Take the 2-minute tour ×

C supports concatenating constant strings at compile-time. Can I do the same for any constant array? (E.g. concatenate two char ** arrays.)

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Basically no, but you can always workaround this with the preprocessor. The trick is to define arrays without curly braces:

#define ARRAY_ONE "test1", "test2", "test3"
#define ARRAY_TWO "testa", "testb", "testc"

Now, you can join arrays in compile time with comma. To use them, however, you will either have to sorround them with curly braces or use a macro:

#define ARR(...) {__VA_ARGS__}

You can now use a single array or any concatenation you need like so:

char *arr1[] = ARR(ARRAY_ONE);
char *arr2[] = ARR(ARRAY_TWO);
char *arrc1[] = ARR(ARRAY_ONE, ARRAY_TWO);

Using macros to achieve such results is bad practice, though.

share|improve this answer
Why do you claim that using macros this way is bad practice? –  hlovdal Nov 19 '12 at 10:02
I basically think that using macros which alter the syntax is always bad practice, because it's very error-prone. Which doesn't mean one shouldn't use them, as that's what they're for, among other uses. Everything is good if you know what you're doing;) –  Michał Trybus Nov 19 '12 at 15:52
Ah, so your point is that ARR(ARRAY_ONE, ARRAY_TWO); reads like an array of two elements while it actually has six. Such confusion is absolutely important to avoid; I recommend to add LIST as part of the identifiers to make it clear that it expands to more than one element, e.g. char *arrc1[] = { ARRAY_ONE_LIST, ARRAY_TWO_LIST };. –  hlovdal Nov 21 '12 at 8:15

Short answer: No.

Longer short answer: String literals are indeed character arrays, but not all character arrays are string literals. The compile-time concatenation works only for string literals. No arrays have that "feature".

share|improve this answer


share|improve this answer

Your Answer


