简体   繁体   中英

#define in C with curly braces

I found a new form of "#define" in C but I don't understand what it means. This is source code:

#define OPT_SET_INT(s, l, v, h, i)  { OPTION_SET_INT, (s), (l), (v), NULL, \
           (h), PARSE_OPT_NOARG, NULL, (i) }

This is the definition of OPTION_SET_INT:

enum parse_opt_type {
    /* special types */
    OPTION_END,
    OPTION_ARGUMENT,
    OPTION_GROUP,
    OPTION_NUMBER,
    /* options with no arguments */
    OPTION_BIT,
    OPTION_NEGBIT,
    OPTION_COUNTUP,
    OPTION_SET_INT,
    OPTION_SET_PTR,
    OPTION_CMDMODE,
    /* options with arguments (usually) */
    OPTION_STRING,
    OPTION_INTEGER,
    OPTION_CALLBACK,
    OPTION_LOWLEVEL_CALLBACK,
    OPTION_FILENAME
};

It is in parse-option.h in this repository: https://github.com/git/git

Thanks.

There's nothing special about this whatsoever. Everything after the macro name (and parenthesis) gets plopped in-place verbatim, with the exception of the macro parameters, which are replaced.

In this case, the macro is used to populate one entry in an array of struct option .

Eg In some C file you might have:

 struct option options[] = {
    OPT_SET_INT(foo, bar, snap, crackle, pop),
    OPT_SET_INT(somethingelse, runningout, offake, names, forthis),
 };

which becomes:

 struct option options[] = {
     { OPTION_SET_INT, foo, bar, snap, NULL, crackle, PARSE_OPT_NOARG, NULL, pop },
     { OPTION_SET_INT, somethingelse, runningout, offake, NULL, names, PARSE_OPT_NOARG, NULL, forthis},
 };

(note the \\ in the macro definition is escaping the newline, so that the definition can span multiple lines).

See GCC Macro Expansion for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM