Strings macro

Discussion in 'C Programming' started by Kalle Anka, Feb 13, 2008.

  1. Kalle Anka

    Kalle Anka Guest

    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
     
    Kalle Anka, Feb 13, 2008
    #1
    1. Advertising

  2. Kalle Anka

    Ian Collins Guest

    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.
     
    Ian Collins, Feb 13, 2008
    #2
    1. Advertising

  3. Kalle Anka

    Martin Guest

    On Feb 13, 6:43 am, 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?


    It might be better to define the arrays as const.

    --
    Martin
     
    Martin, Feb 13, 2008
    #3
  4. 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.
     
    Duncan Muirhead, Feb 13, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,168
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    600
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    522
    Mark McIntyre
    Nov 22, 2005
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    809
    Malcolm
    Jun 24, 2006
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    500
    Patrick Kowalzick
    Mar 14, 2006
Loading...

Share This Page