Santiago Urueña <su*****@gmail. comwrites:
str = (const char *)message;
str = (char *)message;
str = message; /* GCC warning! */
message is a pointer to a function returning const char *. The
previous two casts are covering up a gratuitous mistake!
You are right! Silly me.
Anyway, the compiler doesn't give any warning even if the qualifier is
discarded by the cast:
str = (const char *)message();
str = (char *)message(); /* No warning! */
str = message();
Is this OK?
Please leave attribution lines in place for quoted text (i.e., lines
like "So-and-so <fo*@bar.comwri tes:"). They make it easier to
follow the conversation, and it's just polite to credit people for
their words.
A cast specifies a type conversion, but what it *really* does is tell
the compiler "I know exactly what I'm doing, don't bother me with
warnings". Because of that property, almost all casts should be
viewed with suspicion. Adding a cast for the sole purpose of
silencing a compiler warning is almost always a mistake; the correct
solution is usually to fix the code so the cast isn't required, either
by arranging for things to be of the desired type in the first place
or by using types that are converted implicitly.
One of the few cases where a cast is necessary is for some arguments
to variadic functions like printf(). For non-variadic functions, the
compiler knows the required type and is able to generate an implicit
conversion if necessary. For a variadic function, the compiler
doesn't necessarily have this information, so you have to give it some
help.
--
Keith Thompson (The_Other_Keit h)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"