🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #172

Re: Scalars and Structs

#172
From: Norman Diamond
Date: Fri, 31 Jul 1992 11:07
27 lines
1866 bytes
In article <1992Jul30.205601.13276@pasteur.Berkeley.EDU> johnm@cory.Berkeley.EDU (John D. Mitchell) writes:
>ANSI Section 3.3.4 Cast Operators:
>Am I correct in taking this to mean that there is no way to cast a
>structure of known size to a scalar that just so happens to have the same
>size (or vice versa)?

Yes.

>Is the right thing to do is to use a union?

I think so.  A strictly conforming program cannot store into one element of
a union and then fetch out of a differently typed element, and the standard
does not guarantee how thorough the overlap is.  But in practice, you will
be more successful in writing implementation-dependent code that depends on
this rather than depending on extensions to the cast operation.
(Even if a conforming implementation extends the cast operation, it still
has to issue a diagnostic.)

>Or is there a better solution.

If the structure doesn't have storage class register, then you can do
*(scalar_type*)&structure.  If the scalar doesn't have storage class
register, then you can do *(structure_type*)&scalar.
--
Norman Diamond       diamond@jit081.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
"Yeah -- bad wiring.  That was probably it.  Very bad."
Technical Details
Message-ID: <1992Jul31.110737.27892@nntpd.lkg.dec.com>
Path: arc1-ca.pugleaf.net!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
References: <1992Jul30.205601.13276@pasteur.Berkeley.EDU>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!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
From: diamond@jit345.bad.jit.dec.com (Norman Diamond)
Subject: Re: Scalars and Structs
Message-ID: <1992Jul31.110737.27892@nntpd.lkg.dec.com>
Keywords: casts
Sender: usenet@nntpd.lkg.dec.com (USENET News System)
Reply-To: diamond@jit.dec.com (Norman Diamond)
Organization: Digital Equipment Corporation Japan , Tokyo
References: <1992Jul30.205601.13276@pasteur.Berkeley.EDU>
Date: Fri, 31 Jul 1992 11:07:37 GMT
Lines: 27