sizeof()样式问题 [英] sizeof() style question

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

问题描述

有人说sizeof(类型)和其他说sizeof(变量)。

为什么?

解决方案

克里斯托弗C. Stacy写道:

有人说sizeof(类型)和其他说sizeof(变量)。
为什么?




你的意思是:可以将sizeof运算符应用于

强制转换表达式,即(类型),或表达式,例如

变量或*变量。两者都是有效用途,在两种情况下

你得到表达式类型的大小(以字节为单位)。


后者特别方便与

malloc():


类型* p;

p = malloc(some_number * sizeof * p);


适用于任意类型的类型,而sizeof(类型)工作

只能保证,因为p是指向类型的指针。


在* p周围使用括号表示混淆。如果你想要括号,请使用:(sizeof * p)。

干杯

Michael

-

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


2005年6月9日星期四22:15:38 GMT, cs****@news.dtpq.com (Christopher C.

Stacy )写道:

有人说sizeof(类型)和其他说sizeof(变量)。
为什么?




有些人是对的,有些人错了,我猜? ;-)


一元运算符的语法非常清楚 -


sizeof unary-expression

sizeof(类型名称)


当然,在括号中放置一个表达式没有任何效果,所以

形式都可以。虽然我不对这两种形式都使用括号,但我怀疑使用括号可能会减少潜在错误

类型sizeof类型。

-


祝福,

Bob


Christopher C. Stacy写道:

有人说sizeof(类型)和其他说sizeof(变量)。
为什么?




sizeof运算符有两种形式。从标准:


sizeof运算符产生其

操作数的大小(以字节为单位),这可能是一个表达式或带括号的

类型名称。大小由

操作数的类型决定。结果是整数。如果

操作数的类型是可变长度数组类型,则操作数是

已计算;否则,操作数不会被评估,

结果是一个整数常量。


所以要么

sizeof(type)< br $> b $ b或

sizeof表达

是正确的,视情况而定。

有些人,原因只知道他们坚持括号

表达式,但

sizeof(表达式)



sizeof表达式

意思完全相同。请注意''sizeof variable''只是''sizeof expression''的特殊情况。

在现实世界中,''sizeof expression''很多更有用,因为它可以在表达式没有永久地绑定到

特定类型的情况下使用



考虑一下这里经常被问到的简单情况:你如何确定
现在范围内声明为数组的数组中元素的数量?宏...
#define NUMBER_OF_ELEMENTS(x)(sizeof(x)/ sizeof *(x))

可以采用数组名称而不考虑其元素的类型。

BTW,关于宏中x的括号与扩展

宏有关,而不是与sizeof的操作数有关。


Some people say sizeof(type) and other say sizeof(variable).
Why?

解决方案

Christopher C. Stacy wrote:

Some people say sizeof(type) and other say sizeof(variable).
Why?



You mean: One can apply the sizeof operator either to a
cast expression, i.e. (type), or to an expression, e.g
variable or *variable. Both are valid uses, in both cases
you get the size (in bytes) of the type of the expression.

The latter is especially convenient in conjunction with
malloc():

type *p;
p = malloc(some_number * sizeof *p);

works for arbitrary type "type", whereas sizeof (type) works
only as long guaranteed as p is a pointer to type.

Using parentheses around *p is a sign of confusion. If you
want parentheses, use: (sizeof *p).
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


On Thu, 09 Jun 2005 22:15:38 GMT, cs****@news.dtpq.com (Christopher C.
Stacy) wrote:

Some people say sizeof(type) and other say sizeof(variable).
Why?



Some people are right, and some people are wrong, I guess? ;-)

The syntax for unary operators is pretty clear -

sizeof unary-expression
sizeof(type-name)

Of course, putting an expression in parenthesis has no effect so both
forms are ok. Although I do not use parentheses for both forms, I
suspect that using parentheses probably cuts down on potential errors
of the type sizeof type.
--

Best wishes,

Bob


Christopher C. Stacy wrote:

Some people say sizeof(type) and other say sizeof(variable).
Why?



The sizeof operator has two forms. From the standard:

The sizeof operator yields the size (in bytes) of its
operand, which may be an expression or the parenthesized
name of a type. The size is determined from the type of the
operand. The result is an integer. If the type of the
operand is a variable length array type, the operand is
evaluated; otherwise, the operand is not evaluated and the
result is an integer constant.

So either
sizeof(type)
or
sizeof expression
is correct, depending on the situation.
Some people, for reasons known only to them, insist on parenthesizing
the expression, but
sizeof(expression)
and
sizeof expression
mean exactly the same thing. Note that ''sizeof variable'' is only a
special case of ''sizeof expression''.
In the real world, ''sizeof expression'' is much more useful, since it can
be used in situations where the expression is not permanently tied to a
particular type.

Consider a simple situation frequently asked about here: how do you
determine the number of elements in an array declared as an array in the
present scope? The macro
#define NUMBER_OF_ELEMENTS(x) (sizeof(x)/sizeof *(x))
can take the array name without regard for the type of its elements.
BTW, the parentheses about ''x'' in the macro have to do with expansion of
macros, not with the operand of sizeof.


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

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