!!!!! (void) question() !!!!!!

S

sugaray

I've came across function callings like this:

if( expression ) {
(void)function();
(void)function2(a,b,c);
}

so i wondering even if the declaration of the functions was of type
void why still add (void) prefeix ,and if the function was of other
non-void type is there any special use of this kinda calling
convention?

-dreamcatcher
 
M

Mark A. Odell

(e-mail address removed) (sugaray) wrote in

I've came across function callings like this:

if( expression ) {
(void)function();
(void)function2(a,b,c);
}

so i wondering even if the declaration of the functions was of type
void why still add (void) prefeix ,and if the function was of other
non-void type is there any special use of this kinda calling
convention?

This isn't what I'd call a calling convention. Usually the author is just
trying to indicate that he does not care about the return value of a
function. The void cast is not required by C. Lint however will complain
if you don't add the void cast.
 
A

August Derleth

Mark A. Odell said:
(e-mail address removed) (sugaray) wrote in



This isn't what I'd call a calling convention. Usually the author is just
trying to indicate that he does not care about the return value of a
function. The void cast is not required by C. Lint however will complain
if you don't add the void cast.

If the author is creating lint-clean code out of compulsion, he should
restrain himself when writing code for a tutorial book. Not only is it
ugly, but it could introduce topics too far ahead of time for the code
to be easily followed by a pupil reading sequentially, as opposed to
an old hand skimming around for a specific section. If the author is
throwing void around before introducing the idea of an incomplete
type, he's setting the neophyte up for confusion.

Of course, a section on how to handle lint and how to get an idea of
which warnings one can ignore in a given program would be very
helpful, and I've never seen a text to provide that info. But, as I
said, everything in its proper time. (On a personal note, I always
thought it nitpicky for lint to complain that I'm not using the retval
of every subroutine I call. Dammit, give my human experience some
credit!)

If, on the other hand, the author doesn't give two damns about lint
and is simply illustrating to the newbie how to safely `throw away' a
retval, he should make it clear what he's doing and explain how
casting to an incomplete type allows interesting things to occur.
After all, it /is/ a cast he's doing, and a cast with nothing on the
`other side' could be a challenge to someone who's just learning the
type system.

In any case, the code is correct as far as it goes. Even if expression
were a variable of an integral type, it would be compilable (tho lint
would mutter). I probably would not enjoy the book sugaray is reading,
but I didn't pay for it.
 
M

Mark McIntyre

I've came across function callings like this:

(void)function();

normally this is done to silence complaints from Lint about "unused
return value from function".
 
N

Nils Petter Vaskinn

If the author is creating lint-clean code out of compulsion, he should
restrain himself when writing code for a tutorial book. [snip]
I probably would not enjoy the book sugaray is reading,
but I didn't pay for it.

Where did sugaray say he got the example out of a book?
 
E

Ed Morton

sugaray said:
I've came across function callings like this:

if( expression ) {
(void)function();
(void)function2(a,b,c);
}

so i wondering even if the declaration of the functions was of type
void why still add (void) prefeix

While it doesn't do any functional harm, it is totally unnecessary and
implies that the author is just casually casting everything to void
which can hide bugs. For example, if someone in future changes
"function()" to return a value instead of void, then having cast it's
return type to "void" would stop lint from warning you to take a look at
this line if you had just forgotten to modify it.

,and if the function was of other
non-void type is there any special use of this kinda calling
convention?

Think long and hard before casting a return type to void. While there
are times when this is appropriate, functions that return a value
usually do so for a good reason and ignoring those can often be a bug
which lint would tell you about if you don't do the cast to void.

Ed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top