Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #5312

Re: Puzzling #define

From falk@rahul.net (Edward A. Falk)
Newsgroups comp.lang.c
Subject Re: Puzzling #define
Date 2011-06-02 22:09 +0000
Organization a2i communications
Message-ID <is91na$ark$1@blue.rahul.net> (permalink)
References <is901c$gf0$1@speranza.aioe.org> <53dbaa2b-5fe0-4ce3-9696-7b812787d389@u19g2000vbi.googlegroups.com>

Show all headers | View raw


In article <53dbaa2b-5fe0-4ce3-9696-7b812787d389@u19g2000vbi.googlegroups.com>,
christian.bau <christian.bau@cbau.wanadoo.co.uk> wrote:
>On Jun 2, 10:41 pm, Edward Rutherford
><edward.p.rutherfor...@REMOVETHIS.gmail.com> wrote:
>> I'm having trouble grasping the meaning behind the following:
>>
>> #define MAKE(z, w) (int)(&(((z *)0)->w))
>>
>> Can anyone fill me in?
>>
>> Specifically, I'm confused on the ((z *)0) part.  I'm not exactly sure
>> what that would give me.

Taken as a whole, the macro returns the byte offset of element 'w'
within an object of type 'z'.  Why they chose to name it MAKE is
a mystery to me.

Expanding: ((z *)0) is zero, cast as a pointer to z.  &(((z *)0)->w)
is the address of element 'w' within a 'z' object.  Because this
particular 'z' object starts at zero, this pointer is also the
byte address of 'w'.  The (int) cast causes the whole macro to return
an integer value of the byte address.

This macro makes some assumptions about the underlying CPU architecture.
If you look up offsetof() in Wikipedia, you'll find a somewhat more
portable definition of offsetof() along with a discussion about
the undefined behavior that christian.bau mentioned.

-- 
	-Ed Falk, falk@despams.r.us.com
	http://thespamdiaries.blogspot.com/

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Puzzling #define Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2011-06-02 21:41 +0000
  Re: Puzzling #define "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-06-02 14:45 -0700
    Re: Puzzling #define falk@rahul.net (Edward A. Falk) - 2011-06-02 22:09 +0000
      Re: Puzzling #define Shao Miller <sha0.miller@gmail.com> - 2011-06-02 19:33 -0500
        Re: Puzzling #define Shao Miller <sha0.miller@gmail.com> - 2011-06-02 22:37 -0500
        Re: Puzzling #define "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-06-03 07:36 -0700
          Re: Puzzling #define Shao Miller <sha0.miller@gmail.com> - 2011-06-03 11:33 -0400
  Re: Puzzling #define "Joel C. Salomon" <joelcsalomon@gmail.com> - 2011-06-02 18:21 -0400
    Re: Puzzling #define Shao Miller <sha0.miller@gmail.com> - 2011-06-02 19:47 -0500
      Re: Puzzling #define "Joel C. Salomon" <joelcsalomon@gmail.com> - 2011-06-02 20:49 -0400
    Re: Puzzling #define Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-02 18:39 -0700
      Re: Puzzling #define Shao Miller <sha0.miller@gmail.com> - 2011-06-02 21:55 -0500
    Re: Puzzling #define "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-06-02 22:34 -0400

csiph-web