C struct 数组默认值

Pau*_*Rox 0 c arrays struct initialization

我有这个结构:

typedef struct {
    GPIO_TypeDef* GPIO_Reg;
    uint16_t GPIO_Pin;
    uint16_t status;
} PinType;
Run Code Online (Sandbox Code Playgroud)

然后,如果我声明这个数组:

PinType array[10];
Run Code Online (Sandbox Code Playgroud)

PinType数组中的元素是用一些默认值初始化的吗?

例如,如果我这样写:

printf("%d", array[1].status);
Run Code Online (Sandbox Code Playgroud)

我应该将其0视为输出吗?还是初始值取决于我声明数组之前的内存内容?

Sou*_*osh 6

这个答案取决于变量的范围。

  • 如果array是全局的,那么它将被自动初始化。
  • 如果arraystatic,则所有元素都将自动初始化为0
  • 如果arrayautomatic存储,则不会自动初始化。