#include <stdio.h> typedef int f(int, ...); int main() { printf("%d,%d", sizeof(printf), sizeof(f)); return 0; }
结果为1,1,见http://codepad.org/fg0r4NhG
解决方案:8分
解决方案:8分
参考gcc或g++中sizeof的实现。
解决方案:8分
$ cat test.c #include <stdio.h> typedef int f(int, ...); int main() { printf("%d,%d", sizeof(printf), sizeof(f)); return 0; } $ gcc --std=c11 --pedantic-errors test.c test.c: In function ‘main’: test.c:5:27: error: invalid application of ‘sizeof’ to a function type [-Wpointer-arith] printf("%d,%d", sizeof(printf), sizeof(f)); ^ test.c:5:44: error: invalid application of ‘sizeof’ to a function type [-Wpointer-arith] printf("%d,%d", sizeof(printf), sizeof(f)); ^ test.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=] printf("%d,%d", sizeof(printf), sizeof(f)); ^ test.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat=] $ gcc --version gcc (Ubuntu 4.9.2-10ubuntu13) 4.9.2 Copyright (C) 2014 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
解决方案:8分
ISO/IEC 9899:1999 (E) ©ISO/IEC
6.5.3.4 The sizeof operator
Constraints
1 The sizeof operator shall not be applied to an expression that has function type or an
incomplete type, to the parenthesized name of such a type, or to an expression that
designates a bit-field member.
C89 (ISO/IEC 9899:1990)本人从来没有见过,非常抱歉。
但是从 http://en.cppreference.com/w/c/language/sizeof 的叙述来看,sizeof 的这一要求从 C89 以来没有发生变化。这个网站对在某版标准开始发生变化的内容都有特别的标注。
6.5.3.4 The sizeof operator
Constraints
1 The sizeof operator shall not be applied to an expression that has function type or an
incomplete type, to the parenthesized name of such a type, or to an expression that
designates a bit-field member.
C89 (ISO/IEC 9899:1990)本人从来没有见过,非常抱歉。
但是从 http://en.cppreference.com/w/c/language/sizeof 的叙述来看,sizeof 的这一要求从 C89 以来没有发生变化。这个网站对在某版标准开始发生变化的内容都有特别的标注。
解决方案:8分
学会使用百度和grep
解决方案:48分