Re: MACRO - How is it working in Linux

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am Freitag, 17. Februar 2006 14:31 schrieb Mukund JB.:
> Dear All,
>
> I have seen a macro like this in the Linux kernel sources in
> include/linux/usb.h line no: 1090.
>
> #define usb_dotoggle(dev, ep, out)  ((dev)->toggle[out] ^= (1 << (ep)))
>
> I did not understand how it works?
> I have written a small usermode program to test this.
>
> #define x 1
> #define y 2
> #define macro() (x = y)
> main()
> {
> 	int abc = macro();
> 	printf("abc = %d\n",abc);
> }
>

Your code is quite different from the one in the kernel. The kernel macro is a 
so called "parameterized macro", meaning that 

	usb_dotoggle(alpha, beta, gamma)

is replaced by:

	((alpha)->toggle[gamma] ^= (1 <<(beta)))

You define a macro without parameters, leading to

	int abc = (1 = 2);

which obviously is not correct C, as you cannot assign something to a constant 
(in other words, 1 is not a lvalue).

I recommend you read some kind of C tutorial on parametrized macros.

- Askadar

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux