Re: Fwd: Question about container_of macro. | |
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] | |
Hi Laurent,
On Jan 18, 2008 5:31 AM, Laurent Pinchart <laurent.pinchart@xxxxxxxxx> wrote:
>
> On Thursday 17 January 2008, Alan Stern wrote:
> > On Thu, 17 Jan 2008, Manish Katiyar wrote:
> > > My question is instead of defining container_of as:
> > >
> > > #define container_of(ptr, type, member) ({ \
> > > const typeof( ((type *)0)->member ) *__mptr = (ptr);
> > > (type *)( (char *)__mptr - offsetof(type,member) );})
> > >
> > >
> > > Why won't this simply work :
> > >
> > > #define container_of(ptr, type, member) ({ \
> > > (type *)( (char *)(ptr) - offsetof(type,member)
> > > );})
> >
> > Indeed, or even this:
> >
> > #define container_of(ptr, type, member) \
> > ((type *) ((char *)(ptr) - offsetof(type,member)))
>
> If I'm not mistaken, that's because
>
> const typeof( ((type *)0)->member ) *__mptr = (ptr);
>
> will warn (or maybe even bail out ?) if ptr is not a pointer to a variable of
> member's type.
>
But even if ptr is not a variable of member's type, in the second line
we are force typecasting it to char * and then subtracting. I guess it
will not warn. I tried in a simple program and it doesn't shout.
Below is the program
#include <stdio.h>
#include <stddef.h>
struct t_s{
int a;
int b;
};
#define container_of(ptr, type, member) \
((type *) ((char *)(ptr) -
offsetof(type,member)))
int main()
{
struct t_s init;
char *c;
init.a = 1;
init.b = 2;
c = (char *)&(init.b);
printf("a = %d\n",(container_of(c,struct t_s,b))->a);
return 0;
}
/home/mkatiyar> gcc -Wall b.c
b.c: In function 'main':
b.c:19: warning: assignment from incompatible pointer type
/home/mkatiyar> ./a.out
a = 1
Thanks
> Laurent Pinchart
>
--
Thanks & Regards,
********************************************
Manish Katiyar ( http://mkatiyar.googlepages.com )
3rd Floor, Fair Winds Block
EGL Software Park
Off Intermediate Ring Road
Bangalore 560071, India
***********************************************
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
linux-usb-devel@xxxxxxxxxxxxxxxxxxxxx
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel
[Home] [Video for Linux] [Photo] [Yosemite Forum] [Yosemite Photos] [Video Projectors] [PDAs] [Hacking TiVo] [Linux Kernel] [Linux SCSI] [XFree86] [Devices] [Big List of Linux Books] [Free Dating]
![]() |