On Fri, 15 Sep 2006 01:12:52 GMT, Andreas Klimas
<kl****@klimas-consulting.comwrote in comp.lang.c:
hello,
no many words, I will start with an example instead
int foo(void *r) {...}
foo() is a function accepting a pointer to void and returning an int.
void test(void) {
int *x=0;
foo(x); /* seems to me as valid */
Due to the prototype for foo() being in scope, the compiler converts
'x' to pointer to void, an allowed automatic conversion, and calls
foo(). No problem.
}
--------------------------------------------------------------
int foo(void(*cb)(void*r)) {...}
This time, foo() is a function that returns an int and accepts a
pointer to a function. That function pointer must point to a function
that accepts a pointer to void and returns nothing.
void bar(int *x) {...}
bar() is a function that returns nothing, but accepts a pointer to
int. The function signature is different.
>
void test(void) {
foo(bar); /* seems to me as valid, but not for the compiler */
}
the question now is hopefully obvious. why can't I assign
a functionpointer where the only difference is void* vs.
int* in its arguments. the direct way is allowed, but the
indirect isn't. do I miss any important point (i think so).
thanks
best wishes
Andreas Klimas
The automatic conversion from "pointer to any object type" to "pointer
to void", and the reverse, is a direct top-level conversion only.
The language does not allow the automatic conversion of
void(*)(object_type*) to void(*)(void*).
The language does not allow the automatic conversion of object_type
**x to void **y.
These are not direct conversions of object_type* to void*.
You can use a cast to do what you want, but note that the actual
parameters passed to the function when it is called through the
pointer must match the function's definition, or the behavior is
undefined.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html