Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: sizeof a struc field Keywords: sizeof Message-ID: <11086@smoke.BRL.MIL> Date: 17 Sep 89 00:06:17 GMT References: <7710@microsoft.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 21 In article <7710@microsoft.UUCP> peterwu@microsoft.UUCP (Peter Wu ) writes: >What's the simplest way to obtain the sizeof a field inside a struc >without declaring a variable? >struct abc { > int def; > char ghi; > long jkl; >}; >I know this works: >#define SIZEGHI sizeof(((struct abc *)0)->ghi) No, that doesn't necessarily work. Review the interminable discussions about use of null pointers. #define SIZEGHI (sizeof(char)) works. There's no way to do this using the member name but not its type, without using some presumed object. However, since the object need not exist for sizeof, you could try #define SIZEGHI (sizeof ((struct abc *)malloc(sizeof(struct abc)))->ghi)