C++ 数组中的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4707942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:13:37  来源:igfitidea点击:

Default values in array

c++arrays

提问by oneat

What are default values for arrays like this:

像这样的数组的默认值是什么:

char c[20];

?

?

BTW, Are there any?

BTW,有吗?

回答by CB Bailey

If declared at namespace scope then cwill have static storage scope and will be zero-initializedso every element of c will have value '\0'.

如果在命名空间范围内声明,那么c将具有静态存储范围并且将被零初始化,因此 c 的每个元素都将具有 value '\0'

If declared in a function then cwill not be initialized. The initial value of the elements of cwill be indeterminate.

如果在函数中声明,则c不会被初始化。的元素的初始值c将是不确定的

回答by Martin Beckett

Undefined or in less technical language - random.

未定义或技术性较低的语言 - 随机。

Your compiler (at least in debug mode) may set it to a particular value for testing.

您的编译器(至少在调试模式下)可能会将其设置为特定值以进行测试。

And of course from a strict Copenhagen interpretation of quantum theory the value it does have is both indeterminate (used in it's correct sense) until you collapse the wavefunction by measuring it.

当然,从对量子理论的严格哥本哈根解释来看,它确实具有的价值都是不确定的(以正确的意义使用),直到您通过测量波函数来折叠波函数。

回答by Edward Strange

This in NOT 'undefined'. It is 'indeterminate'. The values are simply are not known.

这不是“未定义”。它是“不确定的”。这些值根本不知道。

回答by Vladimir

As it is already said - values are indeterminate. But, I have to mention that if your array is static or global, the values are initialized to their default values, which usually means they will be initialized to zeros (or '\0' in the case of array of chars).

正如已经说过的 - 价值是不确定的。但是,我不得不提一下,如果您的数组是静态的或全局的,则这些值将被初始化为其默认值,这通常意味着它们将被初始化为零(或在字符数组的情况下为 '\0')。

EDIT: As Noah Roberts suggested, the term "indeterminate" is probably more appropriate than "undefined" (strictly mathematically speaking) - so suggestion is accepted and I've changed the term to "indeterminate". But majority of us are engineers or programmers here, not mathematicians (I suppose) and similar omissions should be forgiven :))

编辑:正如诺亚·罗伯茨所建议的那样,“不确定”一词可能比“未定义”(严格来说是数学上的)更合适——所以建议被接受,我已将术语更改为“不确定”。但是我们这里的大多数人都是工程师或程序员,而不是数学家(我想),应该原谅类似的遗漏:))