Reputation: 45
I bumped into this macro definition.
#define CMD_CTX (cmd->ctx)
I understand its functionality. it is trying to access the member 'ctx' from struct 'command_invocation' in the following snippet. I knew that because 'command_invocation' is the only struct that has 'ctx' member in my code. what's confusing is why there was no mention for the type of the input argument.
struct command_invocation {
struct command_context *ctx;
struct command *current;
const char *name;
unsigned argc;
const char **argv;
};
Now the call of CMD_CTX is as follows
command_context_mode(CMD_CTX, COMMAND_EXEC);
and the definition of this function is
int command_context_mode(struct command_context *cmd_ctx, enum command_mode mode)
I understand the return value matches. But what's not clear for me here is how the input argument is determined in this call "command_context_mode(CMD_CTX, COMMAND_EXEC);"
Upvotes: 2
Views: 852
Reputation: 12354
macros are just named text fragments. So, they just simply gets replaced by their contents before real compilation by a pre-processor. So in your case the call to the function
command_context_mode(CMD_CTX,
will be replaced with
command_context_mode((cmd->ctx),
Note that '(' and ')' from the macro definition will be here as well. I intentionally did not show the second parameter to the call because it looks like yet another macro which you provided no definition for.
So, macros do not need to know anything about the types, since they only replace text. But the compiler will check for the type matching after the replacement happened.
IMHO, this type of macro use only makes the program more difficult to read and more confusing (as it did for you). Do not use it.
Upvotes: 1