sizeof参数 [英] sizeof argument

查看:109
本文介绍了sizeof参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个编译没有任何警告,什么,如果有的话,是什么意思,

以及为什么程序打印出1?


#包括< stdio.h>


int main(无效)

{

printf("%d \ n" ;,sizeof(int()));

返回0;

}


我知道我可能意味着 sizeof(int),而不是sizeof(int())。我只是好好的。
好​​奇。


提前感谢任何解释。

Why does this compile with no warnings, what, if anything, does it mean,
and why does the program print out 1?

#include <stdio.h>

int main(void)
{
printf("%d\n", sizeof(int()));
return 0;
}

I know I probably "meant" sizeof (int), not sizeof (int()). I''m just
curious.

Thanks in advance for any explanations.

推荐答案

Ben C写道:
为什么这个编译没有任何警告,什么,如果有的话,是什么意思,
为什么程序打印出1?

#include< stdio.h>

int main(void)
{/> printf("%d \ n",sizeof(int( )));
返回0;
}

我知道我可能意味着 sizeof(int),而不是sizeof(int())。我只是好奇。
Why does this compile with no warnings, what, if anything, does it mean,
and why does the program print out 1?

#include <stdio.h>

int main(void)
{
printf("%d\n", sizeof(int()));
return 0;
}

I know I probably "meant" sizeof (int), not sizeof (int()). I''m just
curious.




好​​的。 ;-)


''int()''实际上是一种函数类型。 (一个不带参数的函数

并返回一个int。)


函数类型的sizeof是未定义的行为。无论是返还

1还是10000,都没关系。这没有任何意义。


使用适当的编译器选项,您应该收到警告:

,例如GCC:

gcc -Wall -pedantic


Test1.c:在函数main中:

Test1.c:5:警告:无效将sizeof应用于函数类型



Good one. ;-)

''int ()'' is actually a function type. (A function taking no argument
and returning an int.)

sizeof on a function type is undefined behavior. Whether it returns
1 or 10000, doesn''t matter. It makes no sense.

With a appropriate compiler options, you should get a warning:
for instance, with GCC:

gcc -Wall -pedantic

Test1.c: In function `main'':
Test1.c:5: warning: invalid application of `sizeof'' to a function type


Ben C写道:
为什么编译没有警告,什么是否有意思,
为什么程序打印出来?

#include< stdio.h>

int main(void)
{
printf("%d \ n",sizeof(int()));
返回0;
}

我知道我可能意味着 sizeof(int),而不是sizeof(int())。我只是好奇。
Why does this compile with no warnings, what, if anything, does it mean,
and why does the program print out 1?

#include <stdio.h>

int main(void)
{
printf("%d\n", sizeof(int()));
return 0;
}

I know I probably "meant" sizeof (int), not sizeof (int()). I''m just
curious.




也许你的警告水平还不够高?对我来说gcc with -Wall

-ansan和-pedantic options报告:

2.c:在函数`main'':

2 .c:5:警告:对函数类型无效的'sizeof''应用


但它在运行时编译并输出1作为输出。



Maybe your warning level is not high enough? For me gcc with -Wall
-ansi and -pedantic options reports:
2.c: In function `main'':
2.c:5: warning: invalid application of `sizeof'' to a function type

But it compiles and prints 1 as output when run.


Ben C写道:
为什么编译没有警告,


因为你没有把你的诊断转为on。

gcc,即使源语言指定为gnu99,

警告


在函数''main''中:

5:警告:将''sizeof''无效应用于函数类型

5:警告:格式''%d''期望类型''int'' ,但是参数2有类型

''long unsigned int''


什么,如果有的话,这是什么意思,


什么都没有,除非您的实施提供了非便携式扩展程序

以及程序原理为什么1出1?


,因为这是你实现的决定解释这个

非便携式构造的含义。

#include< ; stdio.h>

int main(void)
{/> printf("%d \ n",sizeof(int()));
返回0;
}

我知道我可能意味着 sizeof(int),而不是sizeof(int())。我只是好奇。

提前感谢任何解释。
Why does this compile with no warnings,
Because you have not got your diagnositics turned on.
gcc, even with the source language specified as gnu99,
warns

In function ''main'':
5: warning: invalid application of ''sizeof'' to a function type
5: warning: format ''%d'' expects type ''int'', but argument 2 has type
''long unsigned int''

what, if anything, does it mean,
nothing, except as a non-portable extension provided by your implementation
and why does the program print out 1?
because that''s what you implementation decided to interpret this
non-portable construct as meaning.

#include <stdio.h>

int main(void)
{
printf("%d\n", sizeof(int()));
return 0;
}

I know I probably "meant" sizeof (int), not sizeof (int()). I''m just
curious.

Thanks in advance for any explanations.




比尔收藏家写道提前谢谢"它是用于支持
的语言,在合规期望中创造支配地位。有礼貌的人

不使用它。



Bill collectors write "thanks in advance." It is language used to
create dominance in the expectation of compliance. Polite human beings
do not use it.


这篇关于sizeof参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆