psm@manta.NOSC.MIL (Scot Mcintosh) (03/06/90)
The following seems to be illegal (at least under Microsoft C version 5.1): (int)array[1] = 5; while this appears to be ok: *(int *)&array[1] = 5; Question: why is the first form illegal, and is there a more 'elegant' way than the second form to accomplish the same thing? -- ---- Scot McIntosh Internet: psm@helios.nosc.mil UUCP: {ihnp4,akgua,decvax,decwest,ucbvax}!sdscvax!nosc!psm
henry@utzoo.uucp (Henry Spencer) (03/07/90)
In article <1025@manta.NOSC.MIL> psm@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@zoo.toronto.edu
throopw@sheol.UUCP (Wayne Throop) (03/11/90)
> From: henry@utzoo.uucp (Henry Spencer) >> From: psm@manta.nosc.mil.UUCP (Scot Mcintosh) >>The following seems to be illegal... >>(int)array[1] = 5; >>[...]Question: why is [..this..] illegal... > Why should it be legal? What does it *mean*? What Henry left out was that a cast is a conversion of datatype. It is a commonly made mistake to suppose that casts change the type of an object or value without any conversion action sometimes. This is never the case. This has never been the case. Once upon a time, a family of sloppy compilers left the result of a cast an lvalue in some circumstances. Some users of this family of compilers have taken this unfortunate circumstance as Natural Law, ignoring or never obtaining lint's warnings to the contrary. Similar to the naive person who takes the local mores of that person's tribe as Natural Law. Thus, the expression ((int)array[1] = 5) is as meaningless in C as the expression ((a+3) = 5). Thus, Henry's amazement. After all, as he said... What does it *mean*? -- Wayne Throop <backbone>!mcnc!rti!sheol!throopw or sheol!throopw@rti.rti.org