By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,805 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,805 IT Pros & Developers. It's quick & easy.

Strings macro

P: n/a
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
Feb 13 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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?
Should be, there's nothing tricky going on there.

--
Ian Collins.
Feb 13 '08 #2

P: n/a
On Feb 13, 6:43*am, Kalle Anka <kalle.a...@ankeborg.sewrote:
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.

--
Martin
Feb 13 '08 #3

P: n/a
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.

Feb 13 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.