Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Strings macro

Reply
Thread Tools

Strings macro

 
 
Kalle Anka
Guest
Posts: n/a
 
      02-13-2008
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-13-2008
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.
 
Reply With Quote
 
 
 
 
Martin
Guest
Posts: n/a
 
      02-13-2008
On Feb 13, 6:43*am, Kalle Anka <(E-Mail Removed)> 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?


It might be better to define the arrays as const.

--
Martin
 
Reply With Quote
 
Duncan Muirhead
Guest
Posts: n/a
 
      02-13-2008
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Macro lens on a camera with a macro setting??? mitchell.chris@gmail.com Digital Photography 2 09-28-2005 07:55 AM
in S.E. Asia : Canon EOS 300d with 100 macro ED vs. Nikon D70 with Nikon 105 macro ? J. Cod Digital Photography 0 09-29-2004 05:46 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments