Sunday, 15 September 2013

What type is a function name in C?

What type is a function name in C?

I always understood that in C, func and &func were equivalent. I assume
they should both be of type pointer, which is 8 bytes on my Win64 system.
However, I just tried this:
#include <stdio.h>
int func(int x, int y)
{
printf("hello\n");
}
int main()
{
printf("%d, %d\n", sizeof(&func), sizeof(func));
return 0;
}
And expecting to get the output 8, 8 was surprised to get 8, 1 instead.
Why is this? What type exactly is func? It seems to be of type char or
some equivalent.
What is going on here?
I compiled this with gcc -std=c99 if it makes a difference.

No comments:

Post a Comment