Path: utzoo!dciem!dretor!nrcaer!sce!cognos!brunog From: brunog@cognos.uucp (Bruno Godbout) Newsgroups: comp.std.c Subject: Constant expressions in initializers Message-ID: <5716@cognos.UUCP> Date: 29 Mar 89 00:05:11 GMT Reply-To: brunog@cognos.UUCP (Bruno Godbout) Organization: Cognos Inc., Ottawa, Canada Lines: 48 I need some help with the ANSI definition of a "constant expression" inside an initializer. I am trying to compile a piece of code that is supposed to initialize a structure member with the value of the "offsetof" macro and the compiler gives me an error. The error message says that "the initializer must be a constant expression". I am using a C compiler which is said to be consistent with the Draft Proposed ANSI standard for C. The compiler defines the "offsetof" macro (in ) as follows: -------- #define offsetof(structype,field) \ ( (size_t)(char *) &(((structype *)0)->field) ) -------- Here is a simplified example of what I am trying to do: -------- struct dummy { size_t s; float f; }; struct dummy Dummy = { offsetof(struct dummy,f), 2.0 }; -------- The compiler tells me that "the initializer must be a constant expression" and flags the element initialized by offsetof. I compiled this code successfully with other compilers. Thinking that this might be relevant to ANSI compilers only, I tried it with "gcc -ansi" and with "gcc -ansi -pedantic" and it still compiled fine. I would like to know if this code is rejected by the ANSI definition or if it is only a restriction of the compiler I am using. Thank you for your help. -- Bruno Godbout 3755 Riverside Dr. VOICE: (613) 738-1338 ext.5086 Cognos, Inc P.O. Box 9707 FAX: (613) 738-0002 Ottawa, Ontario CANADA K1G 3Z4 UUCP: uunet!mitel!sce!cognos!brunog