Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!rutgers!cmcl2!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: offsetof() macro Message-ID: <10822@smoke.BRL.MIL> Date: 26 Aug 89 11:40:58 GMT References: <70@motto.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 34 In article <70@motto.UUCP> dave@motto.UUCP (dave brown) writes: >1. Am I right about such a macro being proposed? >2. Was it accepted? Yes. >3. What arguments does it take? What value does it return? #include defines the offsetof macro, which has two arguments: the first designates a structure type, and the second is a member designator. It returns the offset (in bytes) of the member of such a structure. >4. Can it be written for all compilers? Can one version be portable, > or would different versions have to be written for different compilers? In general, some help from the compiler is required. This could take the form of a keyword __offsetof in terms of which the offsetof macro could be defined. An almost-portable definition is possible, using a single extern struct and a lot of type casts and so forth. You may also have seen a definition that involves using 0 as a pseudo-structure pointer; that's less likely to work. >5. Can it be used in a static initializer, ex: > size_t mbr_off = offsetof(...); Frankly, I don't recall, and my copy of the standard is not at hand. >5. If it could be portable, can you supply a definition. If not, can you > supply one which would work in most cases? I'm sure you'll get zillions of suggestions here..