Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of using parentheses in macros

Tags:

c

linux

macros

I am curious to know the use of parentheses for both filp and x pointers in the following assignment operation:

#define init_sync_kiocb(x, filp) \
do { \
struct task_struct *tsk = current; \
(x)->ki_flags = 0; \
(x)->ki_users = 1; \
(x)->ki_key = KIOCB_SYNC_KEY; \
(x)->ki_filp = (filp); \        // This line here
....
....

Source: https://github.com/gp-b2g/gp-peak-kernel/blob/master/include/linux/aio.h#L135

like image 497
Ven Avatar asked Jan 27 '26 17:01

Ven


1 Answers

These are used in a macro definition which is handled by the preprocessor as text substitution. The fact that it is text substitution can result in weird expressions. Consider:

p = &a_struct_array[10];
init_sync_kiocb(p + 20, filp)

without the parens, it turns into:

p + 20->ki_filp = (filp); 

with the parens:

(p + 20)->ki_filp = (filp);

I couldn't, but I bet similar examples can be found for the filp too, or at least you never know for sure.

like image 129
perreal Avatar answered Jan 30 '26 09:01

perreal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!