难道"静态/的extern uint8_t有数组[2] = {0};"符合ANSI C规范? [英] Does "static/extern uint8_t array[2] = {0};" conform to the ANSI C specification?

查看:1200
本文介绍了难道"静态/的extern uint8_t有数组[2] = {0};"符合ANSI C规范?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经就以下code的一个问题:

I've a question regarding the following code:

#include "all_needed.h"

static uint8_t array[2] = {0};

void main(void)
{
  ...
}

时的一个(模块)的全球获准进行初始化如上面有每个成员设置为零而被ANSI C符合阵列?

Is a (module) global array allowed to be initialized as above for having each member set to zero while being ANSI C conform?

我有在code作曲5(MSP430项目),我不得不把它转变成

I've got a problem in Code Composer 5 (MSP430 Project) where I had to change it into

static uint8_t array[2] = {0, 0};

有该阵列的第2构件的正确初始化

for a correct initialization of the 2nd member of the array.

推荐答案

是的,这是允许的,并且该数组应该初始化为零。 C99,P10§6.7.8:

Yes, this is allowed, and should initialize the array to zero. C99, §6.7.8 p10:

如果有静态存储持续时间的对象未初始化
  明确,则:

If an object that has static storage duration is not initialized explicitly, then:

- 如果它有指针的类型,它被初始化为空指针;

— if it has pointer type, it is initialized to a null pointer;

- 如果它具有算术类型,它被初始化为(正或无符号的)零;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

- 如果它是集料,每件被初始化(递归地)根据这些规则;

— if it is an aggregate, every member is initialized (recursively) according to these rules;

- 如果它是联合,第一名为构件被初始化(递归地)根据这些规则

— if it is a union, the first named member is initialized (recursively) according to these rules.

和P21

如果有一个大括号括起来的列表更少的初始化值多于使用比在数组中的元素来初始化已知大小的数组元素或聚集的成员,或者更少的字符的字符串,则剩余合计应初始化隐含一样具有静态存储持续时间的对象。

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

报告的bug你的编译器供应商。

Report the bug to your compiler vendor.

这篇关于难道"静态/的extern uint8_t有数组[2] = {0};"符合ANSI C规范?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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