在gcc编译器中sizeof(main)
,sizeof(printf)
和sizeof(scanf)
所有这些都是1.我想知道所有这些的大小是多少1.它背后的逻辑是什么?
ken*_*ytm 22
因为C(99)标准要求(§6.5.3.4/ 1)
sizeof
不应将运算符应用于具有函数类型或不完整类型的表达式,此类型的带括号的名称,或应用于指定位字段成员的表达式.
所以返回值毫无意义.如果需要sizeof
函数指针,请使用
sizeof(&main)
sizeof(&printf)
sizeof(&scanf)
Run Code Online (Sandbox Code Playgroud)
gcc在sizeof无意义的类型上返回1(参见c-common.c):
4187 if (type_code == FUNCTION_TYPE)
4188 {
4189 if (is_sizeof)
4190 {
4191 if (complain && (pedantic || warn_pointer_arith))
4192 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4193 "invalid application of %<sizeof%> to a function type");
4194 else if (!complain)
4195 return error_mark_node;
4196 value = size_one_node;
4197 }
4198 else
4199 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4200 }
4201 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4202 {
4203 if (type_code == VOID_TYPE
4204 && complain && (pedantic || warn_pointer_arith))
4205 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4206 "invalid application of %qs to a void type", op_name);
4207 else if (!complain)
4208 return error_mark_node;
4209 value = size_one_node;
4210 }
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2448 次 |
最近记录: |