Casting a postdecrement operand

Davidsen davidsen at steinmetz.UUCP
Fri Jun 13 02:52:03 AEST 1986


In article <1273 at ulysses.UUCP> jss at ulysses.UUCP writes:
>In article <114 at romp.UUCP> lwh at romp.UUCP (lwh) writes:
>>My version of pcc on the IBM RT PC allows the following expression:
>>
>>		((struct abc *)cbap)++;
>>
>>
>>to increment cbap by 500. It appears that the ANSI standard doesn't say
>>anything about the legality of this syntax.  
>
>"++" requires an lvalue. A cast produces a value but not an lvalue,
>so this is not legal in ANSI (or in K&R ) C. 
>
>Jerry Schwarz
>Bell Labs, MH

The variable 'cbap' is a pointer. The cast '(struct abc *)' is a
pointer cast. I can use '((struct abc *)cbap)' with indirects and
subscripts and anywhere that I can use an Lvalue, so I really believe
that the expression is legal.

Now, as to being *portable*. There is some language in the X3J11
proposed standard about possible loss of information and
non-portability when pointers are cast. This is why the 'void *' type
was introduced (in part) to have a pointer which could hold any valid
data address. However, there is no guarantee that when the address of
one data type, however obtaining, is cast to a pointer to another data
type that the result will be valid.

If I recall correctly the term in use in X3J11 for stuff was "valid but
non-portable". Sorry I can't remember for sure who said it. I hope this
clarifies the question somewhat (it does in my mind, anyway).
-- 
	-bill davidsen

  ihnp4!seismo!rochester!steinmetz!--\
                                       \
                    unirot ------------->---> crdos1!davidsen
                                       /
         sixhub ---------------------/        (davidsen at ge-crd.ARPA)

"Stupidity, like virtue, is its own reward"



More information about the Comp.lang.c mailing list