0

I'm having difficulty understanding codes,

and the code goes like this,

#define MOBJ_ARGS \
struct struct_name *struct_name, \
int (*getvalue)(struct struct_name *struct_name, other variables), \
//other variables....


struct struct_name {
int (*mobj)(MOBJ_ARGS);
}

first of all, I have no idea what that define does. is it that MOBJ_ARGS is defined as some kind of structure and have all that variables inside, or MOBJ_ARGS could be interpreted as any of those variables?

I have never seen any structure like this before, and i can't even google this since I don't know what this is called :(

plz help me

  • `#define NAME` and whatever follows is simply creating an alias of whatever follows called `NAME`. So during compilation, everywhere the compiler sees `NAME` is replaces it with whatever follows. It is referred to as a macro. Since macros cannot be spread over multiple lines the `'\'` is used as a line continuation. See [GNU Macro](https://gcc.gnu.org/onlinedocs/cpp/Macros.html) for a good discussion, especially the [Macro Pitfalls](https://gcc.gnu.org/onlinedocs/cpp/Macro-Pitfalls.html#Macro-Pitfalls) part. – David C. Rankin May 06 '21 at 04:28
  • Does this answer your question? [Why do multi-line macros have backslashes at the end of each line?](https://stackoverflow.com/questions/19765674/why-do-multi-line-macros-have-backslashes-at-the-end-of-each-line) – phuclv May 06 '21 at 04:32
  • [What does '\' do in C?](https://stackoverflow.com/q/67393671/995714) – phuclv May 06 '21 at 12:49

1 Answers1

0

Preprocessor command #define is used to declare macro which has a name and some list (sequence) of tokens which is just a "text"(can be empty). Also there is possible to define function-like macrose with input paremters. If preprocesser finds a defined name of macro in the source code it just replaced it with the list of token.

In your example there is a macro with name MOBJ_ARGS and list of tokens

struct struct_name *struct_name, \
int (*getvalue)(struct struct_name *struct_name, other variables), \
//other variables....

and preprocessor replaces name "MOBJ_ARGS" with these tokens. So after macro "substitution" the code

struct struct_name {
    int (*mobj)(MOBJ_ARGS);
}

may looks like

struct struct_name {
    int (*mobj)(
        struct struct_name *struct_name, \
        int (*getvalue)(struct struct_name *struct_name, other variables), \
        //other variables....
    );
}

or simplier without using line continuation symbols ""

struct struct_name {
    int (*mobj)(struct struct_name *struct_name, int (*getvalue)(struct struct_name *struct_name, other variables), //other variables....
    );
}

In other words this macro is used to give a "name" to some "text" which is used as a list of arguments in the funciton definitions/declarations

Does this make sense for you?

Speechkin
  • 41
  • 1
  • Thank you very much for your answer. just one more question, if I were to write, I would probably use structure, but I am curious which would be more efficient. – NamHyunKang May 07 '21 at 04:17