Re: Preprocessor automation to define/declare array of strings

Discussion in 'C Programming' started by Ike Naar, Aug 6, 2012.

  1. Ike Naar

    Ike Naar Guest

    On 2012-08-06, pozz <> wrote:
    > I was thinking to automate the declaration/definition of lists of strings
    > through C preprocessor. For example:
    >
    > --- defstrings.def ---
    > STRINGLIST(strlist_month, { "Jan", "Feb", ... } );
    > STRINGLIST(strlist_day, { "Mon", "Thu", ... } );
    > STRINGLIST(strlist_onoff, { "ON", "OFF", ... } );
    > --- defstrings.c ---
    > #define STRINGLIST(name, list) const char *name[] = list;


    This will not work. The macro takes two parameters,
    but it is called with more than two arguments, e.g. in

    STRINGLIST(strlist_onoff, { "ON", "OFF" } );

    there are three arguments,

    strlist_onoff /* first */
    { "ON" /* second */
    "OFF" } /* third */

    This could be fixed using a variable arguments macro,

    #define STRINGLIST(name, ...) const char *name[] = __VA_ARGS__;

    Another issue: perhaps it would be better to drop the terminating
    semicolon from the macro definition, because if the macro is called
    the way you do it, the result will have two terminating semicolons
    which may cause syntax errors.
     
    Ike Naar, Aug 6, 2012
    #1
    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. Cronus
    Replies:
    1
    Views:
    715
    Paul Mensonides
    Jul 15, 2004
  2. marekdec
    Replies:
    0
    Views:
    365
    marekdec
    Jan 5, 2007
  3. apondu
    Replies:
    0
    Views:
    626
    apondu
    Jul 19, 2007
  4. Eric Sosman
    Replies:
    9
    Views:
    671
    Tim Rentsch
    Sep 7, 2012
  5. Jorgen Grahn
    Replies:
    1
    Views:
    360
    Stefan Ram
    Aug 11, 2012
Loading...

Share This Page