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

Discussion in 'C Programming' started by sugaray, Oct 14, 2003.

  1. sugaray

    sugaray Guest

    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
    sugaray, Oct 14, 2003
    #1
    1. Advertising

  2. (sugaray) wrote in
    news::

    > 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.

    --
    - Mark ->
    --
    Mark A. Odell, Oct 14, 2003
    #2
    1. Advertising

  3. Re: !!!!! (void) question() !!!!!! [OT]

    "Mark A. Odell" <> wrote in message news:<Xns94148AAF8CEB9CopyrightMarkOdell@130.133.1.4>...
    > (sugaray) wrote in
    > news::
    >
    > > 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.


    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.
    August Derleth, Oct 14, 2003
    #3
  4. On 14 Oct 2003 10:31:47 -0700, in comp.lang.c ,
    (sugaray) wrote:

    >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".


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Oct 14, 2003
    #4
  5. Re: !!!!! (void) question() !!!!!! [OT]

    On Tue, 14 Oct 2003 15:04:00 -0700, August Derleth wrote:

    > 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?

    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up
    Nils Petter Vaskinn, Oct 15, 2003
    #5
  6. sugaray

    Ed Morton Guest

    sugaray wrote:

    > 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.

    > -dreamcatcher
    Ed Morton, Oct 15, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    494
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    767
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    806
    S.Tobias
    Jul 22, 2005
  4. Giannis Papadopoulos

    void vs void* (philosophical question)

    Giannis Papadopoulos, Jun 30, 2006, in forum: C Programming
    Replies:
    18
    Views:
    499
    Dave Thompson
    Jul 10, 2006
  5. Replies:
    1
    Views:
    382
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page