Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Typecast to (void)

Reply
Thread Tools

Typecast to (void)

 
 
Billy Mays
Guest
Posts: n/a
 
      06-29-2010
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.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-29-2010
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) kst- <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"
 
Reply With Quote
 
 
 
 
Nick
Guest
Posts: n/a
 
      06-29-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: typecast error! Mark Fitzpatrick ASP .Net 1 02-08-2006 04:45 AM
Re: typecast error! Otis Mukinfus ASP .Net 1 02-04-2006 05:32 PM
Re: typecast error! Joe Van Meer ASP .Net 1 02-02-2006 02:00 AM
generics typecast Tomba Java 8 01-13-2006 02:02 AM
typecast int to string Venkat C++ 9 01-08-2004 01:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57