0

I need to #define a macros as a function. For example:

#define REGISTER 0x80000000
...
writel(addr, nic->regs + REGISTER); // arguments are address and register

I defined it like that:

#define WRITEL(addr, nic->reg + reg) ((writel(addr, nic->regs + (reg))))

What's wrong here? Thanks

1
  • Do you have a paticular reason to do it as a macro, not the normal function with all the benefits (type checking ...... etc etc)? Commented Jul 14, 2017 at 10:36

1 Answer 1

7

Macro arguments are a bit like normal function arguments. On the left side you just need a name that is used on the right

 #define WRITEL(ADDR, REG) (writel(ADDR, nic->regs + (REG)))
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! Never did these things. I think I got the principle. I really appreciate your help
But is it necessary to cover the function with () ?
@GeorgeZ. it is not.
Sorry @tilz0R is correct. It was muscle memory. Any other expression than a single bare function call would require the parentheses.
@tilz0R That's a good point. Keeping the parentheses is defensive.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.