Strings macro

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kalle Anka

    Strings macro

    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
  • Ian Collins

    #2
    Re: Strings macro

    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.

    Comment

    • Martin

      #3
      Re: Strings macro

      On Feb 13, 6:43 am, Kalle Anka <kalle.a...@ank eborg.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

      Comment

      • Duncan Muirhead

        #4
        Re: Strings macro

        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.

        Comment

        Working...