Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!mcgill-vision!bloom-beacon!mintaka!think!samsung!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!batata.huji.ac.il!amoss From: amoss@batata.huji.ac.il (amos shapira) Newsgroups: comp.lang.c Subject: Re: offsetof() operator. A tricky question. Message-ID: <695@shuldig.Huji.Ac.IL> Date: 13 Mar 90 06:08:13 GMT References: <8ega02MX90Tk01@amdahl.uts.amdahl.com> Sender: daemon@ucbvax.BERKELEY.EDU Lines: 36 vohra@uts.amdahl.com (Pavan Vohra) writes: >I would like to compile in the offsets of structure members. >For example, with this structure > struct astruct { > int member0; > char member1; > } myastruct; >I want to have, in the same program that the structure appears in, >the number of bytes past myastruct where I can find member1. The answer could be simple and I used several times, do this: struct astruct *ap; ap = NULL; offsetof_member0 = &ap->member0; /* zero offset */ offsetof_member1 = &ap->member1; /* sizeof(member0) */ An offsetof() operator would be quite handy here. But until then you can use this with a good portability of your programs. Now comes to mind, maybe you could avoid the nbull pointer variable with this (didn't try it): offsetof_member0 = &((struct astruct *)NULL->member0); offsetof_member1 = &((struct astruct *)NULL->member1); - Amos Shapira amoss@batata.bitnet amoss@batata.huji.ac.il