用逗号初始化变量 [英] Initialising variables with the comma

查看:58
本文介绍了用逗号初始化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


使用ANSI C ...


以下代码引起了一些麻烦......


UINT16 x,y,z = 0

人们会期望x和y和z设置为0吗?

欢呼

m

Hello

Using ANSI C...

The following code is causing a bit of bother....

UINT16 x, y, z = 0

Would people expect x and y and z to be set to 0 ?

cheers
m

推荐答案

" pod" < ma ************** @ googlemail.comwrote:
"pod" <ma**************@googlemail.comwrote:

以下代码引起了一些麻烦...... 。


UINT16 x,y,z = 0

人们期望x和y和z设置为0吗?
The following code is causing a bit of bother....

UINT16 x, y, z = 0

Would people expect x and y and z to be set to 0 ?



取决于。如果此行发生在任何函数定义之外(因此具有

文件范围,因此具有静态持续时间),则默认的静态

对象初始化将它们设置为0。它在一个函数内部定义,然后(因为缺少一个明确的静态关键字)

它们是自动对象,因此必须假设包含

垃圾值。


Richard

Depends. If this line occurs outside any function definition (hence with
file scope, and therefore with static duration), then the default static
object initialisation will set them to 0. If it''s inside a function
definition, then (because of the lack of an explicit static keyword)
they''re automatic objects, and must therefore be assumed to contain
garbage values.

Richard


3月2日下午2:39,"荚果" < marcus.d.poll ... @ googlemail.comwrote:
On Mar 2, 2:39 pm, "pod" <marcus.d.poll...@googlemail.comwrote:

Hello


使用ANSI C ...


以下代码引起了一些麻烦....


UINT16 x,y,z = 0


人们会期望x和y和z设置为0吗?


欢呼

m
Hello

Using ANSI C...

The following code is causing a bit of bother....

UINT16 x, y, z = 0

Would people expect x and y and z to be set to 0 ?

cheers
m



如果他们有自动存储持续时间,就像我不希望的那样


char * x,y,z; / * whitespace ommited以避免样式参数* /


分配三个指针。如果用文件范围声明,似乎

可能会将它们全部初始化为0,无论是否有b $ b那里是否有= 0(尽管我可以不知道UINT16的

定义(我可以猜,但这与

知道的不一样),而且我遇到过破坏的实现未能完成

这个初始化所以我通常手工初始化。)

-

ais523

Not if they have automatic storage duration, just as I wouldn''t expect

char*x,y,z; /* whitespace ommited to avoid style arguments */

to allocate three pointers. If declared with file scope, it seems
likely that they would all be initialised to 0 regardless of whether
there''s an =0 there or not (although I can''t tell without knowing the
definition of UINT16 (I can guess, but that''s not the same as
knowing), and I''ve come across broken implementations that fail to do
this initialisation so I generally initialise by hand anyway).
--
ais523


在文章< 45 **************** @ news.xs4all.nl>,

Richard Bos< rl *@hoekstra-uitgeverij.nlwrote:
In article <45****************@news.xs4all.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:

>" pod" < ma ************** @ googlemail.comwrote:
>"pod" <ma**************@googlemail.comwrote:

>以下代码引起了一些麻烦。 ......

UINT16 x,y,z = 0

人们会期望x和y和z设置为0吗?
>The following code is causing a bit of bother....

UINT16 x, y, z = 0

Would people expect x and y and z to be set to 0 ?


取决于。如果此行发生在任何函数定义之外(因此具有
文件范围,因此具有静态持续时间),则默认的静态对象初始化将它们设置为0.如果它在函数内部
定义,然后(因为缺少一个显式的静态关键字)
它们是自动对象,因此必须假设包含垃圾值。

理查德


Depends. If this line occurs outside any function definition (hence with
file scope, and therefore with static duration), then the default static
object initialisation will set them to 0. If it''s inside a function
definition, then (because of the lack of an explicit static keyword)
they''re automatic objects, and must therefore be assumed to contain
garbage values.

Richard



当然,没有人在这里解决真正的问题,那就是上面的

线并不是(唉)意思您认为这意味着什么(在C中)。


它的意思是:


UINT16 x

UINT16 y

UINT16 z = 0

(暂时搁置缺少的分号)

Of course, nobody is addressing the real point here, which is that the
line above doesn''t (alas) mean what you think it means (in C).

What it does mean is:

UINT16 x
UINT16 y
UINT16 z = 0

(leaving aside for the moment the missing semicolons)


这篇关于用逗号初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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