Article View: comp.std.c
Article #4403Re: "Size not needed?"
From: Richard Krehbiel
Date: Tue, 28 Nov 1995 19:00
Date: Tue, 28 Nov 1995 19:00
25 lines
1495 bytes
1495 bytes
I wrote:
>Well, actually I'd probably use a macro that I've declared for my own
>use...
>#define CONTAINER_P(p,s,m) (s*)((char *)(p)-offsetof(s,m))
>...which, for pointer p which is assumed to be a pointer to member m
>of aggregate s, gives a pointer to s. There's a slight bit more type
>checking than the simple cast (I'd love it if I could also be assured
--------------------------------------
>that p is really the same type as &s.m). If the member is first,
--------------------------------------
>typically the subtraction is optimized away since the offset is a
>constant zero.
I've tested the following and it does just what I want:
#define CONTAINER_PTR(p,s,m) \
((sizeof(&((s *)0)->m == (p))), \
(s *) ((char *)(p) - offsetof(s,m)))
--
Richard Krehbiel, Kastle Systems, Arlington VA USA
rich@kastle.com (work) or richk@cais.com (personal)
Technical Details
Message-ID:
<1995Nov28.185702.4482@friend.kastle.com>Path:
arc1-ca.pugleaf.net!uu4news.netcom.com!friend!newsReferences:
<48gu0r$91o@news0.rain.rg.net> <1995Nov17.120551.23493@friend.kastle.com> <48iljp$jl5@news0.rain.rg.net> <DI9nH2.DIy@kroete2.freinet.de> <1995Nov20.125350.9627@friend.kastle.com>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!uu4news.netcom.com!friend!news From: rich@kastle.com (Richard Krehbiel) Subject: Re: "Size not needed?" Message-ID: <1995Nov28.185702.4482@friend.kastle.com> Sender: news@friend.kastle.com (News) Reply-To: rich@kastle.com Organization: Kastle Development Associates X-Newsreader: Forte Free Agent 1.0.82 References: <48gu0r$91o@news0.rain.rg.net> <1995Nov17.120551.23493@friend.kastle.com> <48iljp$jl5@news0.rain.rg.net> <DI9nH2.DIy@kroete2.freinet.de> <1995Nov20.125350.9627@friend.kastle.com> Date: Tue, 28 Nov 1995 19:00:25 GMT