home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19515 < prev    next >
Encoding:
Text File  |  1993-01-10  |  2.0 KB  |  75 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!munnari.oz.au!yarrina.connect.com.au!warrane.connect.com.au!g2syd!roberts
  3. From: roberts@g2syd.genasys.com.au (Robert Swan)
  4. Subject: offsetof problems on DEC
  5. Message-ID: <1993Jan11.001937.26507@g2syd.genasys.com.au>
  6. Organization: Genasys II, Sydney, Australia
  7. Date: Mon, 11 Jan 1993 00:19:37 GMT
  8. Lines: 65
  9.  
  10. Greetings.
  11.  
  12. When is a constant not a constant?  When you're compiling on a DEC
  13. (Ultrix 4.3, C++/ANSI C compiler).  Enough of the (dull) wit then, here
  14. is my problem.
  15.  
  16. Compiling the following code on the DEC generates an error message that
  17. the case expression is not a constant...  strange that the same
  18. expression is sufficiently constant to satisfy the earlier static
  19. initialisation.
  20.  
  21. =================================================================
  22. #include <stddef.h>
  23.  
  24. typedef struct
  25. {
  26.     int    bag;
  27. }
  28. SCUM;
  29.  
  30. static int trash = offsetof(SCUM, bag);
  31.  
  32. int
  33. main(int argc,char * argv[])
  34. {
  35.     switch (trash)
  36.     {
  37.     case offsetof(SCUM, bag):
  38.         break;
  39.     }
  40.  
  41.     return (0);
  42. }
  43. =================================================================
  44.  
  45.  
  46. I've heard reports of problems with compilers (DEC, SGI, SCO ?)  not
  47. handling offsetof with particular array elements e.g.  in
  48.  
  49.     struct foo
  50.     {
  51.         char bar[4][20];
  52.     };
  53.  
  54.     offsetof(foo, bar) is considered constant, but
  55.     offsetof(foo, bar[2]) is not.
  56.  
  57. I suspect that my problem is related to this. Just when can the result of
  58. the offsetof macro (used sensibly) be anything but constant?  While I
  59. hesitate to point the finger at the compiler, this sure looks like a
  60. compiler error to me (I know that HP, Sun and IBM have no complaints
  61. about the same code).
  62.  
  63. Just as an aside, wouldn't it make more sense for offsetof to be of type
  64. ptrdiff_t rather than size_t?  I usually want to use the offset, not the
  65. size of the sub-structure...
  66.  
  67. Has anyone else had problems with offsetof?
  68.  
  69. Have fun,
  70.  
  71. Robert.
  72. -- 
  73. I used to be indecisive,   | Robert Swan, roberts@g2syd.genasys.com.au
  74. but now I'm not so sure.   | Genasys II Pty. Ltd., North Sydney.
  75.