我可以像这样初始化一个char数组变量 [英] can i initialize a char array variable like this

查看:108
本文介绍了我可以像这样初始化一个char数组变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char a [256] = {0}

没关系?

解决方案

是如果你附加一个分号就可以形成一个语法观点。


char a [256] = {0};


然而,只将第一个元素初始化为0,所有其他

元素都保持未初始化,这可能不是你想要做的b $ b b。如果你想初始化整个数组,你必须初始化每个元素,如


char a [3] = {''a'', ''b'',0};


对于你庞大的数组,我会使用循环或memset来初始化它。


问候


Flo


Flo写道:


是的,如果你附加一个分号就可以形成一个语法观点。


char a [256] = {0};


然而,只将第一个元素初始化为0,所有其他

元素都保持未初始化,这可能不是你想要做的那些



不是这样。所有其他元素都将_default initialized_,这与_uninitialized_相同,而不是
。内置类型的默认初始化器

为零。


问候,

Bart。


Flo写道:


是的,如果你附加一个分号,就可以形成一个语法观点。 />

char a [256] = {0};


然而,只将第一个元素初始化为0,所有其他元素

元素未初始化,[...]



这是不正确的。未给出明确的

初始化的所有元素都是*零初始化*。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


char a[256] = { 0 }
is it ok?

解决方案

Yes, it is ok form a syntax point of view if you append a semicolon.

char a[256] = { 0 } ;

However that initialises only the first element to 0, all other
elements are left uninitialized, what is probably not what you have
intended to do. If you like to initialize the whole array, you have to
initialize each element as in

char a[3] = { ''a'', ''b'', 0 } ;

For your huge array, i would use a loop or memset to initialize it.

Greetings

Flo


Flo wrote:

Yes, it is ok form a syntax point of view if you append a semicolon.

char a[256] = { 0 } ;

However that initialises only the first element to 0, all other
elements are left uninitialized, what is probably not what you have
intended to do.

Not so. All other elements will be _default initialized_ which is not
the same as _uninitialized_. The default initalizer for built-in types
is zero.

Regards,
Bart.


Flo wrote:

Yes, it is ok form a syntax point of view if you append a semicolon.

char a[256] = { 0 } ;

However that initialises only the first element to 0, all other
elements are left uninitialized, [...]

That is incorrect. All elements that are not given an explicit
initialiser are *zero-initialised*.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于我可以像这样初始化一个char数组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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