Why does the following code give:
#include<stdio.h>
int voo()
{
printf ("Some Code");
return 0;
}
int main() {
printf ("%zu", sizeof voo);
return 0;
}
The following output:
1
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The C language does not define
sizeoffor functions. The expressionsizeof vooviolates a constraint, and requires a diagnostic from any conforming C compiler.gcc implements pointer arithmetic on function pointers as an extension. To support this, gcc arbitrarily assumes that the size of a function is 1, so that adding, say, 42 to the address of a function will give you an address 42 bytes beyond the function’s address.
They did the same thing for void, so
sizeof (void)yields 1, and pointer arithmetic onvoid*is permitted.Both features are best avoided if you want to write portable code. Use
-ansi -pedanticor-std=c99 -pedanticto get warnings for this kind of thing.