sizeof没有括号 [英] sizeof without parenthesis

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

问题描述

我遇到的代码包含的内容包括


sizeof int


为什么一个人可以调用sizeof而没有任何括号

它的论点?这是否符合标准?通常可以用一个参数函数完成
吗?无论如何,为什么还要这样做呢?

除了显示一个人知道并保存两个字符外?

解决方案

霍华德布莱斯说:


我遇到的代码包含


sizeof int



不能编译。 sizeof的操作数是表达式或

括号类型。


为什么一个人可以调用sizeof而没有任何括号

它的论点?



这不是一个参数,因为sizeof不是一个函数。这是一个操作员,

取一个操作数。


这是否可以接受标准?



当然可以。


一般来说,一个参数函数可以完成吗?



不,但是sizeof不是函数。


无论如何,为什么还要这样做,

除了显示一个人知道并保存两个字符外?



(为什么(会(想要(给(加(假(括号)))))))))


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


Howard Bryce写道:
< blockquote class =post_quotes>
我遇到的代码包含的内容包括


sizeof int



几乎肯定不是真的。

你可能遇到过像

sizeof myVar


为什么会这样一个人可以在没有任何括号的情况下调用sizeof

它的论点?这是否符合标准?通常可以用一个参数函数完成
吗?无论如何,为什么人们想要这样做呢?除了表明一个人知道并保存两个字符之外,还有b $ b?



sizeof是一个运算符,而不是函数。

它的参数要么是括号中的类型名称,要么是某个表达式。

示例:


在comp.lang.c中使用malloc()的首选方法是

T * p;

....

p = malloc(sizeof * p);

if(p == NULL){

/ * your错误处理* /

}



p = malloc(数字* sizeof * p);

而不是比

p = malloc(sizeof(T));



p = malloc(number * sizeof(T));

因为改变p(和* p分别)的类型需要

没有改编malloc()的参数。


有些人我担心没有任何不明朗的操作数可能会咬他们或害怕他们获得优先权。错了,因此

他们总是写sizeof(某事)。

干杯

Michael

- -

电子邮件:我的是/ at / gmx / dot / de地址。


Howard Bryce< Hb **** @ yahoo.comwrites:


我遇到的代码包含


sizeof int


该特定形式是非法的。


为什么一个人可以在没有任何括号的情况下调用sizeof

它的论点?这是否符合标准?通常可以用一个参数函数完成
吗?无论如何,为什么人们想要这样做呢?除了表明一个人知道并保存两个字符之外,还有b $ b?



" sizeof"它不是一个函数,它是一个内置的一元运算符,如&

或*。它恰好是唯一一个符号为

关键字而不是标点符号的运算符。


语法为:

sizeof unary-expression

sizeof(类型名称)


例如,您可以编写sizeof 42; 42是表达式,并且

" sizeof 42"产生表达式'的类型(int)的大小。


你也可以写sizeof(42)。如果你喜欢。在这种情况下,

括号与sizeof运算符无关;你只是用

申请sizeof括号表达式。这与

相同,与-42相同。与" ;-(42)" ;.


与其他运算符不同,sizeof也可以应用于带括号的

类型名称,如的sizeof(int)的" ;. sizeof int,没有

括号,是语法错误。括号中的sizeof(int)括号中的括号

在语法上与函数调用中的括号没有关系(或者在演员操作中用括号表示
);它们只是sizeof运算符的

语法的一部分。


(函数调用总是需要括号。)


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


I have come across code containing things like

sizeof int

How come that one can invoke sizeof without any parentheses surrounding
its argument? Is this admissible within the standard? Can it in general be
done with one argument functions? Why would one want to do it anyway,
other than showing that one knows and to save two characters?

解决方案

Howard Bryce said:

I have come across code containing things like

sizeof int

which won''t compile. The operand of sizeof is either an expression or a
parenthesised type.

How come that one can invoke sizeof without any parentheses surrounding
its argument?

It''s not an argument, because sizeof is not a function. It''s an operator,
taking an operand.

Is this admissible within the standard?

Yes, of course.

Can it in general be done with one argument functions?

No, but sizeof is not a function.

Why would one want to do it anyway,
other than showing that one knows and to save two characters?

(Why (would (one (want (to (add (spurious (parentheses)))))))?)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Howard Bryce wrote:

I have come across code containing things like

sizeof int

This is almost certainly not true.
You may have come across things like
sizeof myVar

How come that one can invoke sizeof without any parentheses surrounding
its argument? Is this admissible within the standard? Can it in general be
done with one argument functions? Why would one want to do it anyway,
other than showing that one knows and to save two characters?

sizeof is an operator, not a function.
Its argument either is a type name in parentheses or some expression.
Example:

The prefered way to use malloc() in comp.lang.c is
T *p;
....
p = malloc(sizeof *p);
if (p == NULL) {
/* your error handling here */
}
or
p = malloc(number * sizeof *p);
rather than
p = malloc(sizeof (T));
or
p = malloc(number * sizeof (T));
because changing the type of p (and *p, respectively) necessitates
no adaption of the argument of malloc().

Some people are afraid that unparenthesised sizeof operands may
bite them or are afraid that they get the "precedence" wrong, thus
they always write "sizeof (something)".
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Howard Bryce <Hb****@yahoo.comwrites:

I have come across code containing things like

sizeof int

That particular form is illegal.

How come that one can invoke sizeof without any parentheses surrounding
its argument? Is this admissible within the standard? Can it in general be
done with one argument functions? Why would one want to do it anyway,
other than showing that one knows and to save two characters?

"sizeof" is not a function, it''s a built-in unary operator, like "&"
or "*". It just happens to be the only operator whose symbol is a
keyword rather than a punctuation mark.

The syntax is:

sizeof unary-expression
sizeof ( type-name )

For example, you can write "sizeof 42"; 42 is an expression, and
"sizeof 42" yields the size of that expression''s type (int).

You can also write "sizeof(42)" if you like. In this case, the
parentheses aren''t related to the sizeof operator; you''re merely
applying "sizeof" to a parenthesized expression. It''s exactly the
same thing as "-42" vs. "-(42)".

Unlike other operators, sizeof can also be applied to a parenthesized
type name, as in "sizeof(int)". "sizeof int", without the
parentheses, is a syntax error. The parentheses in "sizeof(int)" are
not syntactically related to the parentheses in a function call (or to
the parentheses in a cast operation); they''re merely part of the
syntax of the sizeof operator.

(Function calls always require parentheses.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

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