Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!uflorida!haven!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: A cast pointer is NOT an lvalue!? (ANSI C) Keywords: X3J11, pointers, cast, lvalue Message-ID: <13912@mimsy.UUCP> Date: 8 Oct 88 14:03:12 GMT References: <479@midgard.mn.org> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 57 In article <479@midgard.mn.org> dal@midgard.mn.org (Dale Schumacher) writes: > value = *((long *) argp)++; > >The original 'argp' is typed as a int pointer. Since I'm working closely >with the compiler authors, I was able to talk to them about the compiler >not handling this construct and was told that the X3J11 proposal states >clearly (in a footnote) that the result of a pointer cast is NOT an lvalue! >It appears to late to get this changed in the standard, but I would like >to know WHY. Because a cast changes the format of the pointer. What do you expect `int i; ((float)i)++;' to do? It is true that on a Vax or a Sun, casting one pointer type to another does not in fact alter the bits. But on a Data General MV/10000 series machine, or on some Lisp machines, it *does*, and the new type and value appears only in a register. The register is discarded immediately after the expression, so incrementing it is useless. In other words, while a pointer cast is *implemented* as a pun on many machines, it is in principle a true conversion, and the result is as ephemeral as the result of any other conversion. >As a clearer example of what I consider valid use of a cast >pointer, consider the following: But this *is* valid: > struct big_thing { /* several elements */ } big, *bp; > char *p; [...] > if(p = malloc(sizeof(struct big_thing))) > *((struct big_thing *) p) = big; > >... Why do I need to assign the cast value to an intermediate in >order to dereference it and assign something to it's contents? You do not. The result of a cast is NEVER an rvalue, but the result of an indirection *is* an lvalue. The following is legal, although the results on a Data General are that the pointer is trashed, and the results on most machines is that the integer is trashed: int i; char *p; ... (*(int **)&p)++; *(float *)&i += .1; >It seems to me that the value of any cast to a pointer >type should result in a modifyable lvalue, even if the expression being >cast is not an lvalue, since you could cast thexpression to a pointer >type, assign it to a pointer variable and then dereference it. The result of an indirection is most assuredly not the same as expression being indirected! (Among other things, it has a different type.) -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris