B
Boon
Hello everyone,
AFAIU, void pointers and thing pointers are compatible, in the sense
that it is "legal" to write the following
thing x;
thing *p = &x;
void *q = p;
thing *r = q;
Is my understanding correct?
Consider the following code.
typedef void fun_t(void *);
void dispatch(fun_t fun, void *param)
{
fun(param);
}
void foo(int *p)
{
*p += 1;
}
int main(void)
{
int param = 666;
dispatch(foo, ¶m); /* WARNING HERE */
return 0;
}
foo.c: In function 'main':
foo.c:13: warning: passing argument 1 of 'dispatch' from incompatible
pointer type
"pointer to function taking void * returning void" and "pointer to
function taking int * returning void" are incompatible?
Even though void * and int * are, themselves, compatible?
The simple fix is to have foo take a void * param, and cast as needed.
typedef void fun_t(void *);
void dispatch(fun_t fun, void *param)
{
fun(param);
}
void foo(void *p) /* CHANGE PARAM TYPE HERE */
{
*(int *)p += 1; /* AND CAST HERE */
}
int main(void)
{
int param = 666;
dispatch(foo, ¶m);
return 0;
}
Why is the second program valid, while the first elicits a warning?
Is the first program valid or does it invoke UB?
(I don't like the second form much because it requires a cast.)
Regards.
AFAIU, void pointers and thing pointers are compatible, in the sense
that it is "legal" to write the following
thing x;
thing *p = &x;
void *q = p;
thing *r = q;
Is my understanding correct?
Consider the following code.
typedef void fun_t(void *);
void dispatch(fun_t fun, void *param)
{
fun(param);
}
void foo(int *p)
{
*p += 1;
}
int main(void)
{
int param = 666;
dispatch(foo, ¶m); /* WARNING HERE */
return 0;
}
foo.c: In function 'main':
foo.c:13: warning: passing argument 1 of 'dispatch' from incompatible
pointer type
"pointer to function taking void * returning void" and "pointer to
function taking int * returning void" are incompatible?
Even though void * and int * are, themselves, compatible?
The simple fix is to have foo take a void * param, and cast as needed.
typedef void fun_t(void *);
void dispatch(fun_t fun, void *param)
{
fun(param);
}
void foo(void *p) /* CHANGE PARAM TYPE HERE */
{
*(int *)p += 1; /* AND CAST HERE */
}
int main(void)
{
int param = 666;
dispatch(foo, ¶m);
return 0;
}
Why is the second program valid, while the first elicits a warning?
Is the first program valid or does it invoke UB?
(I don't like the second form much because it requires a cast.)
Regards.