(void)data

T

Tuang

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,stdout);
}
putc('\n',stdout);
return 0;
}


I don't understand the line that says

(void)data;

Could someone explain that line to me?

Thanks.
 
J

Joona I Palaste

Tuang said:
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,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.
 
L

lallous

Joona I Palaste said:
Tuang said:
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,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 ([email protected]) ------------- 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.
 
M

Martin Dickopp

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,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
 
T

Thomas Stegen

Tuang said:
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.
 
K

kevin collins

Joona I Palaste posted this:
Tuang said:
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,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.
 
J

Joona I Palaste

kevin collins said:
Joona I Palaste posted this:
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 ([email protected]) ------------- 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
 
J

Jeremy Yallop

kevin said:
[...]
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.
 
A

Alex Monjushko

Tuang said:
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,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.
 

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,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top