const 数组的默认值(当未显式设置所有索引时)?
我四处搜寻,但没有找到这个问题的答案。我想知道的是:
如果我声明一个 static unsigned char const ARRAY[256] = { [0] =0
处的元素之外的所有元素)都具有特定值,还是它们是随机的?
我需要这个常量查找表,该表不会填充所有元素,但不会太大,因此浪费一些空间不是问题。
欢迎提出更合适的数据类型或约定的建议。
谢谢!
I've searched around and haven't found an answer to this. What I would like to know is this:
If I declare a static unsigned char const ARRAY[256] = { [0] = <some_value> };
,
can I expect the unspecified elements (in this case all but the element at index 0
) to have a certain value, or will they be random?
I need this for a constant lookup table that won't have all elements filled, but isn't too large, so wasting some space isn't a problem.
Suggestions for more appropriate data types or conventions are welcome.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数组的其余部分将被初始化为零。
另外,您可以初始化数组中的多个元素,例如:
将创建数组 arr 并将其初始化为:
The rest of the array will be initialized to zero.
Also, you may initialize more than one element in the array, for example:
will create and initialize the array arr to:
如果您想明确说明这一点并且您正在使用 gnu c,则总是有
...
:If you want to be explicit about it and you're using gnu c, there's always
...
:如果你想要一些特定的行为,那么就以这种方式初始化它。假设编译器的某些怪癖决定了程序的正确操作,这只是自找麻烦。
If you want some particular behaviour, then initialize it that way. Assuming some quirk of the compiler upon which the correct operation of your program is hinged is just asking for trouble.