473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
2 2804
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
16
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
5
by: el prinCipante | last post by:
I'm getting tired of the following error message. Compiler Error message : Error: Need explicit cast to convert from: float to: float * I am trying to use a routine from the Numerical Recipes...
33
by: hermit_crab67 | last post by:
Can someone explain to a C newbie why this doesn't work as I expect it to work? (expectations clearly outlined in the printf statement in main routine) OS: Linux 2.4.26 GCC: 2.95.4 void...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
25
by: hugo2 | last post by:
Obrhy/hugo July 12, 2004 Take a look at this memcpy() definition. Is there a good reason the void pointer args are cast to byte just to assign their addresses to byte pointers? /*from Steve...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.