Re: why do we use while (0)

2007-12-26 Thread Li Zefan
sahlot arvind wrote: Recently I started looking into linux kernel and trying to understand the code. I am working with linux-2.6.9. in file include/llinux/list.h - I found something like this. #define INIT_LIST_HEAD(ptr) do { \ (ptr)-next = (ptr); (ptr)-prev = (ptr); \ } while (0)

Re: why do we use while (0)

2007-12-26 Thread Maneesh Singhal
One more reason could be, by using do-while(0) thingy, one could use 'break' statement at any place just to come out of the macro at once. Thanks. Anupam Kapoor wrote: sahlot arvind [EMAIL PROTECTED] wrote: , | Recently I started looking into linux kernel and trying to understand | the

Re: why do we use while (0)

2007-12-26 Thread Mayank Kaushik
The intent is just to enclose the code of the macro in a block, so that if you were to place this macro after an if() statement, and the macro was multi-line, it would not break things. eg. #define crazy(a, b) \ a = b; \ b = a; if(\*something*\) crazy(a,b) Look at this carefully, which

why do we use while (0)

2007-12-25 Thread sahlot arvind
Recently I started looking into linux kernel and trying to understand the code. I am working with linux-2.6.9. in file include/llinux/list.h - I found something like this. #define INIT_LIST_HEAD(ptr) do { \ (ptr)-next = (ptr); (ptr)-prev = (ptr); \ } while (0) My question is why do we

Re: why do we use while (0)

2007-12-25 Thread dinesh bansal
Its not because of optimization. This is done to convert a multiline expression into a single line expression so that it does not break if-else functionality. On Dec 26, 2007 11:44 AM, sahlot arvind [EMAIL PROTECTED] wrote: Recently I started looking into linux kernel and trying to

Re: why do we use while (0)

2007-12-25 Thread sahlot arvind
I tried this - - struct list_head { struct list_head *next, *prev; }; #define INIT_LIST_HEAD(ptr){(ptr)-next = (ptr); (ptr)-prev = (ptr);} int main () { int i; struct list_head mylist; struct list_head *ptr = mylist; printf (hi\n); if

Re: why do we use while (0)

2007-12-25 Thread sahlot arvind
Yes I get an error in this code - - struct list_head { struct list_head *next, *prev; }; #define INIT_LIST_HEAD(ptr){(ptr)-next = (ptr); (ptr)-prev = (ptr);} int main () { int i; struct list_head mylist; struct list_head *ptr = mylist;

Re: why do we use while (0)

2007-12-25 Thread Li Zefan
sahlot arvind wrote: Yes I get an error in this code - - struct list_head { struct list_head *next, *prev; }; #define INIT_LIST_HEAD(ptr){(ptr)-next = (ptr); (ptr)-prev = (ptr);} int main () { int i; struct list_head mylist; struct

Re: why do we use while (0)

2007-12-25 Thread Anupam Kapoor
sahlot arvind [EMAIL PROTECTED] wrote: , | Recently I started looking into linux kernel and trying to understand | the code. I am working with linux-2.6.9. in file | include/llinux/list.h - I found something like this. | | #define INIT_LIST_HEAD(ptr) do { \ | (ptr)-next = (ptr);