Typecast to (void)

Discussion in 'C Programming' started by Billy Mays, Jun 29, 2010.

  1. Billy Mays

    Billy Mays Guest

    Periodically I see code that looks similar to this:


    void do_something(int a, int b)
    {
    (void) b;
    printf("A is %d\n", a);
    }

    What does this typecast do? Running code like this through lint
    complains the "(void) b" statement does nothing. Any thoughts?


    --
    Billy Mays
    http://www.jpgdump.com <- My attempt at humor.
    Billy Mays, Jun 29, 2010
    #1
    1. Advertising

  2. Billy Mays <> writes:
    > Periodically I see code that looks similar to this:
    >
    > void do_something(int a, int b)
    > {
    > (void) b;
    > printf("A is %d\n", a);
    > }
    >
    > What does this typecast do? Running code like this through lint
    > complains the "(void) b" statement does nothing. Any thoughts?


    It's probably intended to silence a warning about b not being
    used in the function. Whether it actually does so depends on the
    compiler or other tool being used. Some compilers might issue a
    warning if b isn't referred to at all, but shut up if it appears as
    "(void) b;". For whatever version of lint you're using, as you've
    seen, it doesn't work particularly well.

    (Lints typically recognize directives in the form of C comments that
    tell them to inhibit certain warnings. I say "lints" rather than
    "lint" because there are multiple versions of lint, just as there
    are multiple distinct C compilers.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 29, 2010
    #2
    1. Advertising

  3. Billy Mays

    Nick Guest

    Kenneth Brody <> writes:

    > On 6/29/2010 1:50 PM, Billy Mays wrote:
    >> Periodically I see code that looks similar to this:
    >>
    >>
    >> void do_something(int a, int b)
    >> {
    >> (void) b;
    >> printf("A is %d\n", a);
    >> }
    >>
    >> What does this typecast do? Running code like this through lint
    >> complains the "(void) b" statement does nothing. Any thoughts?

    >
    > It's purpose is to "do nothing", just like lint says.
    >
    > Well, to be more precise, it's to "use" the otherwise-unused parameter
    > "b", but not do anything with it.


    And the reasons you might want to do this are

    1) that you intend to add 'b' later (or have eliminated 'b' and don't
    want to change all your header files etc). This isn't a very good
    reason and the second version is particularly weak.

    2) that this is one of a pile of functions with a similar prototype that
    is called from a function pointer - perhaps from a table. This is much
    more reasonable.

    The problem is that many compilers will complain about:
    void do_something(int a, int b)
    {
    printf("A is %d\n", a);
    }
    and some of them can be silenced by adding the
    (void) b;

    Some can't. Many compilers have a non-standard way to do this properly:
    gcc has an __attribute__((unused)) that can be used for the purpose.
    --
    Online waterways route planner | http://canalplan.eu
    Plan trips, see photos, check facilities | http://canalplan.org.uk
    Nick, Jun 29, 2010
    #3
    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:
    526
    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:
    788
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    831
    S.Tobias
    Jul 22, 2005
  4. Replies:
    1
    Views:
    406
    Victor Bazarov
    May 23, 2007
  5. question about void typecast

    , Dec 26, 2007, in forum: C Programming
    Replies:
    8
    Views:
    371
    Stephen Montgomery-Smith
    Dec 27, 2007
Loading...

Share This Page