In C89 can I safely do the following
void function(void *p)
{
FILE *fp = p;
/* ... */
return;
}
as long as I know function() to only be called with valid (FILE*)
arguments?
Yes.
Also (referring to the subject line), this isn't casting, merely
conversion; a cast is a construct in the source code that forces a
conversion:
FILE *fp=(FILE *)p;
^^^^^^^^
This would also be valid in the case you asked about, but in general it's
a Bad Idea to cast a void * to any pointer type, since the conversion
happens without the cast anyways; leaving the cast out, besides saving
some typing and the avoiding the possibility of introducing errors by
casting to the wrong type, avoids problems when you think what you're
casting is a void * but it's actually something else (the compiler will
catch that for you without the cast).
(There are some exceptions, where the compiler doesn't know what type
to convert it to, but those are Rather Rare.)
dave
--
Dave Vandervies (e-mail address removed)
April fools postings certainly have deteriorated since the days
of Kremvax and Moscvax.
As have the fools. --Michael Rubenstein and Joe Maun in comp.lang.c