🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2956

Re: Casting const; Is this standard conforming?

#2956
From: Steve Clamage
Date: Tue, 17 Nov 1992 18:30
27 lines
1350 bytes
rhl@grendel.Princeton.EDU (Robert Lupton the Good) writes:


>   const int i = 0;
>
>   (int)i = 1;				/* Fails */
>   *(int *)&i = 1;			/* OK */
       
>Thinking about ROMs my guess is that gcc and the IBM xlc compilers are
>correct to reject the former assignment, and that their acceptance of
>the latter is permitted, but could anyone comment? 

The first assignment is illegal because a cast yields a value, not
an lvalue, and you may not assign to a value.  It has nothing to do
with const.

The second assignment assigns to an object via a modifiable lvalue, and
so violates no constraint.  Nonetheless, the results of assigning to an
object declared const via a non-const lvalue are undefined.  (ANSI 3.5.3)

You can't rely on any particular result.  The compiler is not obliged
to diagnose the problem, but may do so.  The resulting code might do
what you intended, but need not.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com
Vice Chair, ANSI C++ Committee, X3J16
Technical Details
Message-ID: <1992Nov17.183027.19459@taumet.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!taumet!steve
References: <1992Nov16.200843.5565@Princeton.EDU>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!sparky!uunet!taumet!steve
From: steve@taumet.com (Steve Clamage)
Subject: Re: Casting const; Is this standard conforming?
Message-ID: <1992Nov17.183027.19459@taumet.com>
Organization: TauMetric Corporation
References: <1992Nov16.200843.5565@Princeton.EDU>
Date: Tue, 17 Nov 1992 18:30:27 GMT
Lines: 27