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;
printf ("hi\n");
if (i)
INIT_LIST_HEAD(ptr);
else
{
printf ("jkldkas\n");
}
return 0;
}
--------------------------
Got the reason why while (0) is there.
Thanks man!
- Arvind
On 12/26/07, sahlot arvind <[EMAIL PROTECTED]> wrote:
>
> 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 (i)
> {
> INIT_LIST_HEAD(ptr);
> }
> else
> {
> printf ("jkldkas\n");
> }
> return 0;
> }
> --------------------------
>
> And there is no compilation error.
>
>
>
> On 12/26/07, Li Zefan <[EMAIL PROTECTED]> wrote:
> >
> > 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)
> > >
> > >
> > > My question is why do we use a loop when we actually know that it is
> > not
> > > going to execute more than once? Cannot we simply do -
> > >
> > > #define INIT_LIST_HEAD(ptr) {(ptr)->next = (ptr); (ptr)->prev =
> > (ptr)}
> > >
> > > Do we get some kind of optimization by using while (0)?
> > >
> >
> > with #define INIT_LIST_HEAD(ptr) {(ptr)->next = (ptr); (ptr)->prev =
> > (ptr);}
> >
> > try this:
> >
> > if (...)
> > INIT_LIST_HEAD(ptr);
> > else
> > ...;
> >
> > you will get compilation error:
> >
> > error: expected expression before 'else'
> >
> > see why? expand the macro:
> >
> > if (...)
> > {
> > (ptr)->next = (ptr);
> > (ptr)->prev = (ptr);
> > }; <---here
> > else
> > ...;
> >
>
>