A cast pointer is NOT an lvalue!? (ANSI C)

Dale Schumacher dal at midgard.mn.org
Fri Oct 7 06:09:42 AEST 1988


In porting my standard library routines to a newly developed C compiler,
I ran into a problem in the printf() formatting code.  In order to take
an arbitrary object off of the stack, and move the argument pointer to
the "next" argument, I used a pointer cast construct like this:

   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.  As a clearer example of what I consider valid use of a cast
pointer, consider the following:

    struct big_thing { /* several elements */ } big, *bp;
    char *p;

    if(p = malloc(sizeof(struct big_thing)))
        {
        bp = ((struct big_thing *) p);
        *bp = big;
        }
    if(p = malloc(sizeof(struct big_thing)))
        *((struct big_thing *) p) = big;

The first example, using the intermediate 'bp', is (as I understand it)
valid ANSI C, but the second example is not.  Why do I need to assign the
cast value to an intermediate in order to dereference it and assign something
to it's contents?  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.



More information about the Comp.lang.c mailing list