Strings macro

K

Kalle Anka

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

Ian Collins

Kalle said:
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?
Should be, there's nothing tricky going on there.
 
M

Martin

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?

It might be better to define the arrays as const.
 
D

Duncan Muirhead

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top