home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!munnari.oz.au!yarrina.connect.com.au!warrane.connect.com.au!g2syd!roberts
- From: roberts@g2syd.genasys.com.au (Robert Swan)
- Subject: offsetof problems on DEC
- Message-ID: <1993Jan11.001937.26507@g2syd.genasys.com.au>
- Organization: Genasys II, Sydney, Australia
- Date: Mon, 11 Jan 1993 00:19:37 GMT
- Lines: 65
-
- Greetings.
-
- When is a constant not a constant? When you're compiling on a DEC
- (Ultrix 4.3, C++/ANSI C compiler). Enough of the (dull) wit then, here
- is my problem.
-
- Compiling the following code on the DEC generates an error message that
- the case expression is not a constant... strange that the same
- expression is sufficiently constant to satisfy the earlier static
- initialisation.
-
- =================================================================
- #include <stddef.h>
-
- typedef struct
- {
- int bag;
- }
- SCUM;
-
- static int trash = offsetof(SCUM, bag);
-
- int
- main(int argc,char * argv[])
- {
- switch (trash)
- {
- case offsetof(SCUM, bag):
- break;
- }
-
- return (0);
- }
- =================================================================
-
-
- I've heard reports of problems with compilers (DEC, SGI, SCO ?) not
- handling offsetof with particular array elements e.g. in
-
- struct foo
- {
- char bar[4][20];
- };
-
- offsetof(foo, bar) is considered constant, but
- offsetof(foo, bar[2]) is not.
-
- I suspect that my problem is related to this. Just when can the result of
- the offsetof macro (used sensibly) be anything but constant? While I
- hesitate to point the finger at the compiler, this sure looks like a
- compiler error to me (I know that HP, Sun and IBM have no complaints
- about the same code).
-
- Just as an aside, wouldn't it make more sense for offsetof to be of type
- ptrdiff_t rather than size_t? I usually want to use the offset, not the
- size of the sub-structure...
-
- Has anyone else had problems with offsetof?
-
- Have fun,
-
- Robert.
- --
- I used to be indecisive, | Robert Swan, roberts@g2syd.genasys.com.au
- but now I'm not so sure. | Genasys II Pty. Ltd., North Sydney.
-