S
stremler
begin quoting Richard Heathfield said:(e-mail address removed) said:
Well, for one thing, you can't cast structs or unions. (C99 muddies the
waters a little with compound literals, but the document was certainly
addressing C90, so I'll confine my answer to that.)
/me tests
Hm... indeed. You have to abuse void * instead of casting to get that
sort of thing to happen.
But my rejection of
"anything goes" was really a rebuttal of the very popular idea that
casting is some kind of magic wand that can turn anything into anything
else. It isn't. A cast converts a value from one type to another type, but
makes no guarantees that the conversion makes sense. For example, you can
cast a long int into a pointer to struct tm if you want, but that doesn't
mean you'll get anything useful out of it. In fact, almost all casting in
C is wrong.
I've always thought of a cast as an instruction to the compiler to trust
the programmer about types in this instance; a sort of "No, really, I know
what I'm doing, I want to treat this object as THAT type, truly, trust me."
If actual conversion of data (as opposed to coercion of type) is involved,
yah, it's probably wrong.