home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / std / c / 2395 < prev    next >
Encoding:
Text File  |  1992-07-30  |  1.8 KB  |  41 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!decwrl!deccrl!news.crl.dec.com!pa.dec.com!oct17.dfe.dec.com!ryn.mro4.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond
  3. From: diamond@jit345.bad.jit.dec.com (Norman Diamond)
  4. Subject: Re: Scalars and Structs
  5. Message-ID: <1992Jul31.110737.27892@nntpd.lkg.dec.com>
  6. Keywords: casts
  7. Sender: usenet@nntpd.lkg.dec.com (USENET News System)
  8. Reply-To: diamond@jit.dec.com (Norman Diamond)
  9. Organization: Digital Equipment Corporation Japan , Tokyo
  10. References: <1992Jul30.205601.13276@pasteur.Berkeley.EDU>
  11. Date: Fri, 31 Jul 1992 11:07:37 GMT
  12. Lines: 27
  13.  
  14. In article <1992Jul30.205601.13276@pasteur.Berkeley.EDU> johnm@cory.Berkeley.EDU (John D. Mitchell) writes:
  15. >ANSI Section 3.3.4 Cast Operators:
  16. >Am I correct in taking this to mean that there is no way to cast a
  17. >structure of known size to a scalar that just so happens to have the same
  18. >size (or vice versa)?
  19.  
  20. Yes.
  21.  
  22. >Is the right thing to do is to use a union?
  23.  
  24. I think so.  A strictly conforming program cannot store into one element of
  25. a union and then fetch out of a differently typed element, and the standard
  26. does not guarantee how thorough the overlap is.  But in practice, you will
  27. be more successful in writing implementation-dependent code that depends on
  28. this rather than depending on extensions to the cast operation.
  29. (Even if a conforming implementation extends the cast operation, it still
  30. has to issue a diagnostic.)
  31.  
  32. >Or is there a better solution.
  33.  
  34. If the structure doesn't have storage class register, then you can do
  35. *(scalar_type*)&structure.  If the scalar doesn't have storage class
  36. register, then you can do *(structure_type*)&scalar.
  37. --
  38. Norman Diamond       diamond@jit081.enet.dec.com
  39. If this were the company's opinion, I wouldn't be allowed to post it.
  40. "Yeah -- bad wiring.  That was probably it.  Very bad."
  41.