Casting pointers

brian scearce bls at svl.cdc.com
Fri Jul 20 05:09:24 AEST 1990


volpe at underdog.crd.ge.com (Christopher R Volpe) writes:
>In article <1614 at ghost.stsci.edu>, davids at stsci.EDU (David Silberberg) writes:
>|>If the last line above were replaced by
>|>
>|>	 ptr = (char *)ptr + num_chars;
>|>
>|>it would perform as desired.  Does casting work on an lvalue?

>I'm not sure if casting on an l-value is specified anywhere.

Harbison & Steele (section 7.5.1): "Any permissible conversion may
be invoked by a cast expression.  The result is not an lvalue."

>main()
>{ int i;
>  float f;

>  f=2.0;
>  (float) i = f;
   ^^^^^^^^^^^^^ This is not legal C.
>  printf("i==%d\n",i);
>}

>Compiled with Sun C, this program prints: i==1073741824
>Compiled with gcc, it prints: i==2

It shouldn't compile at all.  The mighty CDC C Version 2 prints,
"Operation can only be performed on lvalues", an excellent diagnostic
if I do say so myself :-)

--
Brian Scearce        \ "I tell you Wellington is a bad general, the English are
(not on CDCs behalf)  \ bad soldiers; we will settle the matter by lunch time."
bls at u02.svl.cdc.com    \   -- Napolean Bonaparte, June 18, 1815 (at Waterloo)
shamash.cdc.com!u02!bls \ From _The Experts Speak_, Cerf & Navasky



More information about the Comp.lang.c mailing list