I was playing around with function pointers, and ran into something that intuitively doesn't fit with my understanding of pointers in C.
Take a look at the following code:
include <stdio.h>
void fun(int a) {
printf("fun, %d!\n", a);
}
void bun(int b) {
printf("bun, %d!\n", b);
}
int main(){
void (*fooptr)(int) = &bun;
void (*blooptr) (int) = fun;
fooptr(200);
blooptr(100);
return 0;
}
If I treated function pointers like any other pointer in C, I should expect void (*blooptr) (int) = fun;
to trigger a compiler warning against incompatible types. My code compiles (silently) and works as expected.
How does C let me initialize a function pointer without the &
operator, like I would do a regular pointer, without even a compiler warning? Is this one of those cases where I'm accidentally getting the right behaviour, though I'm technically in undefined behaviour territory? If someone could explain why this works, with some insight (references to literature will also do) into the nature of function pointers, that would be great.
I tried looking around for this specific question, but couldn't find anything related. Seems a bit fundamental, so apologies if it's a dup.