阵列初始化怀疑。 [英] Array initalization doubt.

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

问题描述

大家好,


考虑这一行 -


char s [30] =" \0" ;;


这会将所有数组元素初始化为零吗?


谢谢。

Hi all,

Consider this line -

char s[30] = "\0";

Does this initialize all array elements to zero ?

Thanks.

推荐答案

在消息< 7f ************************** @ posting.google.com>
km ********** @ yahoo.com (KM Jr.)写道:
In message <7f**************************@posting.google.com >
km**********@yahoo.com (K.M. Jr.) wrote:
大家好,

考虑这一行 -

char s [30] =" \0";
Hi all,

Consider this line -

char s[30] = "\0";

Does this initialize all array elements to zero ?




是的。在C中,一个对象要么完全初始化,要么根本没有初始化。

如果你给一个对象一个初始化器,初始化器未明确填充的任何元素被设置为零件。


-

Kevin Bracey,首席软件工程师

Tematic Ltd电话:+44(0)1223 503464

182-190 Newmarket Road传真:+44(0)1728 727430

剑桥,CB5 8HE,英国WWW: http://www.tematic.com/




KM小" <公里********** @ yahoo.com>在消息中写道

新闻:7f ************************** @ posting.google.c om ...

"K.M. Jr." <km**********@yahoo.com> wrote in message
news:7f**************************@posting.google.c om...
大家好,

考虑这一行 -

char s [30] =" \0";
Hi all,

Consider this line -

char s[30] = "\0";

Does this initialize all array elements to zero ?




标准人员肯定会回答这个问题。我的猜测是响亮的

" no"从某种意义上说,我不会指望它。我认为只有s [0]初始化了




但正如我所说的......问标准人员并希望你的编译符合

标准。



The standards guys will answer that definitively. My guess is a resounding
"no" in the sense that I would not count on it. I think only s[0] gets
initialized.

But as I said... Ask the standards guys and hope your compiler conforms to
the standard.


Kevin Bracey写道:
Kevin Bracey wrote:
km ********** @ yahoo.com (KM Jr.)写道:
km**********@yahoo.com (K.M. Jr.) wrote:
考虑这一行 -

char s [30] =" \0";

这会将所有数组元素初始化为零吗?
Consider this line -

char s[30] = "\0";

Does this initialize all array elements to zero ?



>是的。在C中,对象要么完全初始化要么完全初始化
。如果你给一个对象一个初始化器,那么初始化器明确填充的任何元素都将设置为零。



Yes. In C, an object is either fully initialised or not initialised
at all. If you give an object an initialiser, any elements not
explicitly filled by the initialiser are set to zero.




对于静态对象,是的。对于自动对象,我不会那么肯定,而且我无意通过标准挖掘

找出答案。简单地做任何初始化实际上需要
要容易得多。这听起来像是一个可能的memset工作。


-

Chuck F(cb********@yahoo.com)(cb ********@worldnet.att.net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home。 att.net>使用worldnet地址!



For static objects, yes. For automatic objects I would not be so
sure, and I have no intention of dredging through the standard to
find out. It is much easier to simply do whatever initialization
is actually required. This sounds like a possible job for memset.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


这篇关于阵列初始化怀疑。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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