By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

is casting (void*) to (FILE*) good?

P: n/a
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?

Thank you,

MackS

Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2005-02-17 19:01:31 -0500, "MackS" <ma***********@hotmail.com> said:
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.

--
Clark S. Cox, III
cl*******@gmail.com

Nov 14 '05 #2

P: n/a
In article <11**********************@l41g2000cwc.googlegroups .com>,
MackS <ma***********@hotmail.com> wrote:
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 dj******@csclub.uwaterloo.ca 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
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.