On Wed, 13 Feb 2008 07:43:51 +0100, Kalle Anka wrote:
> I'd like to have a couple of arrays of strings where the first strings
> are the same in all arrays.
> I tried this:
>
> #define COMMON_STRINGS "alfa", "bravo", "charlie"
>
> char *firstStrArr[] = {COMMON_STRINGS, "delta", "echo", "foxtrot"};
> char *secondStrArr[] = {COMMON_STRINGS, "golf", "hotel", "india"};
> char *thirdStrArr[] = {COMMON_STRINGS, "juliett", "kilo", "london"};
>
>
> It works fine when I compile it with gcc, but I always feel little bit
> shaky when it comes to strings and preprocessing, so I wonder if this is
> correct and portable?
>
> Thanks
>
> /Krister
I've used such macros; they seem fine to me. However being clumsy I once
managed to miss the comma after a COMMON_STRINGS invocation. This does not
lead to a syntax error, but to constant string concatenation, and was time
consuming to track down. Since the horse had fled, I bolted the stable
door by using the ugly:
#define COMMON_STRINGS ("alfa"), ("bravo"), ("charlie")
which does yield a syntax error if you miss the comma after an invocation.
|