Lvalues and casts
Gary L. Randolph
randolph at ektools.UUCP
Thu May 25 04:32:37 AEST 1989
In article <1989May23.225818.9602 at utzoo.uucp> henry at utzoo.uucp (Henry Spencer) writes:
>In article <847 at tub.UUCP> net at tub.UUCP (Oliver Laumann) writes:
>> (int *)cp = ip;
>>
>>The PCC, however, says "illegal lvalue in assignment". Is this legal
>>C, i.e. is the result of a cast really an lvalue?
>
>No. Many compilers have accepted it, historically, but it has never
>been legal C and it is not legal ANSI C.
It is my understanding that all of the answers (similar to this one)
are correct, but I have an extension to the original question. I have
used casts on the LHS of assignment, but without actually assigning to
the cast. Example:
func(data, semaphore)
char* data;
char semaphore;
{
if (semaphore=='a')
*(int *)data = 22;
else if (semaphore=='b')
*(float *)data = 64;
.
.
.
}
Now the user of func() has supplied a pointer to some type, not
necessarily a char. semaphore is used to determine the type of
pointer func() actually received. The location pointed to by
data is then modified as it is a legal lvalue. I believe this to
be a typical use of the abused generic pointer, char*. I would,
of course, use void* if I were using a dpANSI compiler. It is my
understanding that using casts on the LHS of assignment in this
way is fully portable. Right???
Gary
More information about the Comp.lang.c
mailing list