简体   繁体   English

具有不足字段的此C / C ++ struct初始化程序如何工作?

[英]How does this C/C++ struct initializer with not enough fields work?

I am looking at some code about which I am puzzled. 我正在看一些我感到困惑的代码。

This snippet is in the header file, RPI.h: 此代码段位于头文件RPI.h中:

#define BCM2708_PERI_BASE  0x20000000
#define GPIO_BASE         (BCM2708_PERI_BASE + 0x200000) // GPIO controller 

// IO Access
struct bcm2835_peripheral {
    unsigned long addr_p;
    int mem_fd;
    void *map;
    volatile unsigned int *addr;
};

struct bcm2835_peripheral gpio = {GPIO_BASE};

extern struct bcm2835_peripheral gpio;

From the RPI.c file: 从RPI.c文件:

#include "RPI.h"

struct bcm2835_peripheral gpio = {GPIO_BASE};

I am puzzled by the line in both the .h and .c files: 我对.h和.c文件中的行感到困惑:

struct bcm2835_peripheral gpio = {GPIO_BASE};

It looks to me that a new struct of type bcm2835_peripheral named gpio is being instantiated and set equal to GPIO_BASE . 在我看来,一个名为gpio bcm2835_peripheral类型的新结构正在被实例化并设置为等于GPIO_BASE However, the struct has four, public members. 但是,该结构有四个公共成员。 Are they all being set to GPIO_BASE ? 它们都被设置为GPIO_BASE吗?

I only included the lines from the .h and .c files which were relevant. 我只包括相关的.h和.c文件中的行。 The lines are in the order they are in the original files. 这些行按原始文件中的顺序排列。

When a struct or array is initialized, you don't have to give an initializer for every element. 初始化结构或数组时,您不必为每个元素提供初始值设定项。

For the initializers that are there, the fields are set in order, and any remaining fields for which there is no initializer are set to 0 for integer types and NULL for pointer types. 对于那里的初始化程序,字段按顺序设置,并且没有初始化程序的任何剩余字段对于整数类型设置为0,对于指针类型设置为NULL

From section 6.7.9 of the C standard : C标准第6.7.9节开始:

10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. 10如果未显式初始化具有自动存储持续时间的对象,则其值不确定。 If an object that has static or thread 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, and any padding is initialized to zero bits; - 如果它是一个聚合,则根据这些规则初始化(递归)每个成员,并将任何填充初始化为零比特;

— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits; - 如果它是一个联合,则根据这些规则初始化(递归)第一个命名成员,并将任何填充初始化为零位;

... ...

21 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. 21 如果括号括起的列表中的初始值设定项少于聚合的元素或成员 ,或者用于初始化已知大小的数组的字符串文字中的字符数少于数组中的元素,则聚合的其余部分应与具有静态存储持续时间的对象隐式初始化。

So in this case the addr_p field is set to GPIO_BASE , while mem_fd , map , and addr are set to 0, NULL , and NULL respectively. 因此,在这种情况下, addr_p字段设置为GPIO_BASE ,而mem_fdmapaddr分别设置为0, NULLNULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM