Casting the lvalue

Henry Spencer henry at utzoo.uucp
Wed Mar 7 08:14:57 AEST 1990


In article <1025 at manta.NOSC.MIL> psm at manta.nosc.mil.UUCP (Scot Mcintosh) writes:
>The following seems to be illegal...
>(int)array[1] = 5;
>while this appears to be ok:
>*(int *)&array[1] = 5;
>
>Question: why is the first form illegal...

Why should it be legal?  What does it *mean*?  To treat array[1] as if
it were an int?  What if it's not the same size or at the same alignment?
If what you *mean* is to take its address, treat that as the address of
an int, and operate on the int that (you hope) is there, that sounds like
the second form to me.

>and is there
>a more 'elegant' way than the second form to accomplish
>the same thing?

Um, just what are you trying to accomplish?  If what you're trying to do
is what the second form does, then no, there is no real alternative to
the second form (barring `*(int *)(array+1)', a minor variation on the
same theme).
-- 
MSDOS, abbrev:  Maybe SomeDay |     Henry Spencer at U of Toronto Zoology
an Operating System.          | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list