当前位置:网站首页>Sizeof (function name) =?

Sizeof (function name) =?

2022-07-05 08:05:00 Car chezi

sizeof( Function name )=?

Go straight to the code

#include <stdio.h>

int foo(void)
{
    
    printf("hello\n");
    return 0;
}

int main(void)
{
    
    printf("%lu\n", sizeof(foo));
    printf("%lu\n", sizeof(&foo));
    printf("%lu\n", sizeof(foo()));
}

Suppose saved as a file 1.c

compile :

$ gcc -Wall -pedantic 1.c -std=c99
 warning: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]
     printf("%lu\n", sizeof(foo));

The result of the operation is :

1
8
4

explain :

-pedantic Options : Show all ISO C and ISO C++ Warning .

about sizeof(foo), I think it should be that function indicators are automatically converted into pointers , The answer should be 8, But not so , Compiler derived 1. It seems that the standard does not allow function indicators to be used as sizeof The number of operations , Or it doesn't make sense .

sizeof(&foo): Function designators are explicitly converted to pointers to functions , So the answer is 8

sizeof(foo()):sizeof The operand of is the return type of the function , So it is 4

【End】

原网站

版权声明
本文为[Car chezi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140546257870.html