By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,394 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

(void)data

P: n/a
I'm looking through samples of C source that are likely to be well
written, like reading Spanish books to improve my Spanish. In the GNU
libiconv source, I found this function:

static int print_one (unsigned int namescount,
const char * const * names,
void* data)
{
unsigned int i;
(void)data;
for (i = 0; i < namescount; i++) {
if (i > 0)
putc(' ',stdout);
fputs(names[i],stdout);
}
putc('\n',stdout);
return 0;
}
I don't understand the line that says

(void)data;

Could someone explain that line to me?

Thanks.
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Tuang <tu******@hotmail.com> scribbled the following:
I'm looking through samples of C source that are likely to be well
written, like reading Spanish books to improve my Spanish. In the GNU
libiconv source, I found this function: static int print_one (unsigned int namescount,
const char * const * names,
void* data)
{
unsigned int i;
(void)data;
for (i = 0; i < namescount; i++) {
if (i > 0)
putc(' ',stdout);
fputs(names[i],stdout);
}
putc('\n',stdout);
return 0;
}
I don't understand the line that says (void)data; Could someone explain that line to me?


It is merely there to make the compiler shut up about the function not
using the parameter "data". It has no effect whatsoever at run-time.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"My absolute aspect is probably..."
- Mato Valtonen
Nov 14 '05 #2

P: n/a
tu******@hotmail.com (Tuang) writes:
I'm looking through samples of C source that are likely to be well
written, like reading Spanish books to improve my Spanish. In the GNU
libiconv source, I found this function:

static int print_one (unsigned int namescount,
const char * const * names,
void* data)
{
unsigned int i;
(void)data;
for (i = 0; i < namescount; i++) {
if (i > 0)
putc(' ',stdout);
fputs(names[i],stdout);
}
putc('\n',stdout);
return 0;
}
I don't understand the line that says

(void)data;

Could someone explain that line to me?


It does nothing. Its most likely purpose is to make the compiler believe
that the function uses the parameter `data', so that it doesn't emit a
warning about an unused parameter.

Martin
Nov 14 '05 #3

P: n/a
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bt**********@oravannahka.helsinki.fi...
Tuang <tu******@hotmail.com> scribbled the following:
I'm looking through samples of C source that are likely to be well
written, like reading Spanish books to improve my Spanish. In the GNU
libiconv source, I found this function:

static int print_one (unsigned int namescount,
const char * const * names,
void* data)
{
unsigned int i;
(void)data;
for (i = 0; i < namescount; i++) {
if (i > 0)
putc(' ',stdout);
fputs(names[i],stdout);
}
putc('\n',stdout);
return 0;
}


I don't understand the line that says

(void)data;

Could someone explain that line to me?


It is merely there to make the compiler shut up about the function not
using the parameter "data". It has no effect whatsoever at run-time.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"My absolute aspect is probably..."
- Mato Valtonen


If I am not wrong that can also be written as:
data = data;

However, if the compiler is set not to optimize the compiled code, will any
of:
"(void) data" or "data = data", have some code generated for it. Though the
code would be of no effect in both cases.

--
Elias
Nov 14 '05 #4

P: n/a
Tuang wrote:
static int print_one (unsigned int namescount,
const char * const * names,
void* data)
{
unsigned int i;
(void)data; [snip]

I don't understand the line that says

(void)data;

Could someone explain that line to me?


It is a no op. It does no useful computation. What it probably
does though is that makes the compiler shut up about
"unused variable data in function print_one" or something.

The reason it is not used and still there can vary. One reason
can be that this function has its prototype specified by something
outside its own control so to speak (to customise a datastructure
or function for example) and so cannot not have it there even though
it does not need it.

--
Thomas.

Nov 14 '05 #5

P: n/a
Joona I Palaste posted this:
Tuang <tu******@hotmail.com> scribbled the following:
I'm looking through samples of C source that are likely to be well
written, like reading Spanish books to improve my Spanish. In the GNU
libiconv source, I found this function:

static int print_one (unsigned int namescount,
const char * const * names,
void* data)
{
unsigned int i;
(void)data;
for (i = 0; i < namescount; i++) {
if (i > 0)
putc(' ',stdout);
fputs(names[i],stdout);
}
putc('\n',stdout);
return 0;
}


I don't understand the line that says

(void)data;

Could someone explain that line to me?


It is merely there to make the compiler shut up about the function not
using the parameter "data". It has no effect whatsoever at run-time.


That looks so strange .... Could someone post the name of that construct
in the BNF in K&R? A page
number too would be a bonus.


Nov 14 '05 #6

P: n/a
kevin collins <ke********@hotmail.com> scribbled the following:
Joona I Palaste posted this:
Tuang <tu******@hotmail.com> scribbled the following:
> I don't understand the line that says
> (void)data;

> Could someone explain that line to me?


It is merely there to make the compiler shut up about the function not
using the parameter "data". It has no effect whatsoever at run-time.

That looks so strange .... Could someone post the name of that construct
in the BNF in K&R? A page
number too would be a bonus.


I don't have K&R handy, but there's nothing strange. It's a cast
expression. A cast expression has the following BNF type:

cast_expression ::= "(" type ")" expression

where _type_ is a C type and _expression_ is a C expression, which
might also be a cast expression.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"A friend of mine is into Voodoo Acupuncture. You don't have to go into her
office. You'll just be walking down the street and... ohh, that's much better!"
- Stephen Wright
Nov 14 '05 #7

P: n/a
kevin collins wrote:
Tuang <tu******@hotmail.com> scribbled the following:
> I don't understand the line that says
> (void)data;

[...] That looks so strange .... Could someone post the name of that construct
in the BNF in K&R? A page
number too would be a bonus.


Here's the derivation according to the grammar in Appendix A
(pp234-239): (You'll need a fixed-width font to view it.)

statement
|
expression-statement
|_______________
| |
expression ';'
|
assignment-expression
|
conditional-expression
|
logical-OR-expression
|
logical-AND-expression
|
inclusive-OR-expression
|
exclusive-OR-expression
|
AND-expression
|
equality-expression
|
relational-expression
|
shift-expression
|
additive-expression
|
multiplicative-expression
|
cast-expression
_______|__________________________
| | | |
'(' type-name ')' cast-expression
| |
specifier-qualifier-list unary-expression
| |
type-specifier postfix-expression
| |
'void' primary-expression
|
identifier ['data']

Jeremy.
Nov 14 '05 #8

P: n/a
Tuang <tu******@hotmail.com> wrote:
I'm looking through samples of C source that are likely to be well
written, like reading Spanish books to improve my Spanish. In the GNU
libiconv source, I found this function: static int print_one (unsigned int namescount,
const char * const * names,
void* data)
{
unsigned int i;
(void)data;
for (i = 0; i < namescount; i++) {
if (i > 0)
putc(' ',stdout);
fputs(names[i],stdout);
}
putc('\n',stdout);
return 0;
}
I don't understand the line that says (void)data;


This is used as a hint to the compiler (as well as the person
maintaining your code) that the 'data' parameter is intentionally
not used. It usually prevents the compiler from issuing warnings
about unused parameters.
--
Alex Monjushko (mo*******@hotmail.com)
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.