A
Alfonso Morra
How can this work ?
I have the following declarations in a header:
Header
=========
typedef void (*VFPTR)(void) ;
void FOO_Callback(void*, char*, char*, int, unsigned long, void*,void*);
int bar(int *, char *, VFPTR, void *);
Source
=========
In the C source code, I come accross this line:
bar(&id, name, FOO_Callback, (void *)handle);
This compiles without any problems when compiling as a C program. When
compiling as a C++ source (stricter type checking etc) - the compiler
(unsuprisingly) threw a wobbly. I had to add an explicit "C-style" cast
as ff:
bar(&id, name, (VOID_FUNC)FOO_Callback, (void *)handle);
and the compiler was happy once again.
BUT - wait a minute, HOW can a function (FOO_Callback) declared as a
function returning void and accepting several arguments, be "coerced"
into a function that accepts no arguments?
Here are my questions:
1). Ok it compiles - but does it make sense?
2). Are there any conditions under which one may coerce a function that
requires arguments into one not taking arguments?
3). What does it mean to call a function that expects arguments, with no
arguments - will it even work ?
I hope someone can shed somelight on this baffling piece of "sleight of
hand".
I have the following declarations in a header:
Header
=========
typedef void (*VFPTR)(void) ;
void FOO_Callback(void*, char*, char*, int, unsigned long, void*,void*);
int bar(int *, char *, VFPTR, void *);
Source
=========
In the C source code, I come accross this line:
bar(&id, name, FOO_Callback, (void *)handle);
This compiles without any problems when compiling as a C program. When
compiling as a C++ source (stricter type checking etc) - the compiler
(unsuprisingly) threw a wobbly. I had to add an explicit "C-style" cast
as ff:
bar(&id, name, (VOID_FUNC)FOO_Callback, (void *)handle);
and the compiler was happy once again.
BUT - wait a minute, HOW can a function (FOO_Callback) declared as a
function returning void and accepting several arguments, be "coerced"
into a function that accepts no arguments?
Here are my questions:
1). Ok it compiles - but does it make sense?
2). Are there any conditions under which one may coerce a function that
requires arguments into one not taking arguments?
3). What does it mean to call a function that expects arguments, with no
arguments - will it even work ?
I hope someone can shed somelight on this baffling piece of "sleight of
hand".