Article View: comp.std.c
Article #2955Re: Casting const; Is this standard conforming?
From: Larry Jones
Date: Tue, 17 Nov 1992 14:44
Date: Tue, 17 Nov 1992 14:44
26 lines
1452 bytes
1452 bytes
In article <1992Nov16.200843.5565@Princeton.EDU>, rhl@grendel.Princeton.EDU (Robert Lupton the Good) writes: > const int i = 0; > > (int)i = 1; /* Fails */ The result of a cast is not an l-value and thus cannot be assigned to. > *(int *)&i = 1; /* OK */ This is acceptable syntax, but results in undefined behavior since i is defined as a const variable. The compiler is allowed to place it in read-only memory, which would cause the assignment to fail. > I want to set > the variable once in a private place just after mallocing it, in case > you wonder why I am trying to get around a const restriction, and > don't see why I can't just initialise it That's a different case than your example: the memory that malloc() returns is *not* defined with the const qualifier, just the pointer that you are using to access it. In that case the second method is well defined and portable. ---- Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH 45150-2789 513-576-2070 larry.jones@sdrc.com or ...uunet!sdrc!larry.jones OK, what's the NEXT amendment say? I know it's in here someplace. -- Calvin
Technical Details
Message-ID:
<2268@sdrc.COM>Path:
arc1-ca.pugleaf.net!sparky!uunet!sdrc!thor!scjonesReferences:
<1992Nov16.200843.5565@Princeton.EDU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!sdrc!thor!scjones From: scjones@thor.sdrc.com (Larry Jones) Newsgroups: comp.std.c Subject: Re: Casting const; Is this standard conforming? Message-ID: <2268@sdrc.COM> Date: 17 Nov 92 14:44:02 GMT References: <1992Nov16.200843.5565@Princeton.EDU> Sender: news@sdrc.COM Lines: 26