Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!compass!worley From: worley@compass.com (Dale Worley) Newsgroups: comp.lang.c Subject: Re: finding offset of a member ... Message-ID: Date: 21 May 91 14:29:48 GMT References: <1285@unisql.UUCP> Sender: root@compass.com Distribution: world Organization: Compass, Inc., Wakefield, MA, U.S.A. Lines: 31 In-reply-to: pckim@unisql.UUCP's message of 20 May 91 20:34:49 GMT In article <1285@unisql.UUCP> pckim@unisql.UUCP (Pyung-Chul Kim) writes: Then, my question is why the compiler does not accept the following expression? char a_array[offsetof(type,member)]; Is it specific to our compiler? Yes. According to the Standard (7 Dec 1988, sec. 4.1.5, p. 99, l. 24) the macro call "offsetof(type, member-designator)" expands to an integral constant expression. Since the [] expression above is allowed to be any integral constant expression (greater than 0), your compiler is broken. (Do, however, make sure that you are including first, since otherwise the offsetof() call is a function call!) I see that the ANSI C provides a macro 'offsetof' (not a compiler operator). It seems that the macro is expanded similarily as I have said. That is, ((size_t)&(((type *)0)->member)) or something like that. Your compiler may do this, but there is no requirement in the Standard on *how* any compiler expands offsetof(), as long as the correct effect is obtained. One plausable approach for a compiler is to treat offsetof() as a "magic macro", that is, one that is recognized by the compiler as an operator, rather than expanding it into a C code which is then processed. Dale Worley Compass, Inc. worley@compass.com -- trump: v. to file for bankruptcy while living on only half a million bucks a month.