Article View: comp.std.c
Article #3448Re: (no subject) [cast expression as an lvalue]
From: Morgan Herrington
Date: Sun, 08 Oct 1995 11:09
Date: Sun, 08 Oct 1995 11:09
47 lines
1810 bytes
1810 bytes
Daniel Wood <dwood> writes:
> The following:
> ((char *)src)++; /* src is an (int *) */
> when compiled using Solaris SparcWorks 3.0.1, generates:
> "t.c", line 12: warning: a cast does not yield an lvalue
>
> I fully understand why it makes no sense to say:
>
> (char *)src = x;
>
> but, if the compiler thinks that it is stupid to interpret:
>
> ((char *)src)++;
> as:
> (char *)src = (char *)src + 1;
>
> then why is it doing it, since I am intending:
>
> src = (char *)src + 1;
Or to avoid a pointer mismatch warning:
src = (int*)((char *)src + 1);
> A cast on an operator like "++" should only be applied to the RHS.
>
> What does the standard say?
"The operand of the postfix increment or decrement operator shall have qualified
or unqualified scalar type and shall be a modifiable lvalue."
> The following isn't pretty but is the only why to get rid of the warning message:
>
> *(char *)(src=(int *)((char *)src+1)) = *(char *)(dst=(int *)((char *)dst+1));
Some compilers relax the language and interpret:
(type)lval = expr;
as:
*(type*)&lval = expr;
So, you could translate your example to:
(*(char**)&src)++;
Morgan Herrington
Technical Details
Message-ID:
<458bh8$5jm@newsworthy.west.sun.com>Path:
arc1-ca.pugleaf.net!newsworthy.West.Sun.COM!usenetReferences:
<4578r1$jub@news.informix.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!newsworthy.West.Sun.COM!usenet From: morgan@nosun.West.Sun.COM (Morgan Herrington) Newsgroups: comp.std.c,comp.lang.c,comp.unix.solaris Subject: Re: (no subject) [cast expression as an lvalue] Date: 8 Oct 1995 11:09:28 GMT Organization: Portland OR, Sun Microsystems Distribution: inet Message-ID: <458bh8$5jm@newsworthy.west.sun.com> References: <4578r1$jub@news.informix.com> Reply-To: morgan@nosun.West.Sun.COM NNTP-Posting-Host: fathom.west.sun.com