部分初始化C结构 [英] Partially initializing a C struct

查看:108
本文介绍了部分初始化C结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接指出:当自动数组或结构具有部分初始化程序时,其余部分将初始化为0.我决定尝试阅读并编写以下代码:

This link states that "When an automatic array or structure has a partial initializer, the remainder is initialized to 0". I decided to try out what I read and wrote the following piece of code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    //int arr[3] = {2};  // line no. 7

    struct s {
        int si;
        int sj;
    };

    struct s myStruct;
    myStruct.si = 9;
    printf("%d\n", myStruct.sj);
}

我不明白为什么在注释掉line no. 7时打印4096(我相信是一些垃圾"值),而当我取消注释line no. 7时却得到0的原因.我不认为arr声明与main()的激活记录(或更确切地说是myStruct)有任何关系(应该提供未注释的line no. 7):

I don't understand why 4096 (which I believe is some "garbage" value) is printed when I comment out line no. 7 and I get 0 when I uncomment line no. 7. I don't think the arr declaration has got something to do with main()'s activation record (or rather myStruct) which should look like (provided we have line no. 7 uncommented):

---------------
|  Saved PC   |
---------------
|  arr[2]     |
---------------
|  arr[1]     |
---------------
|  arr[0]     |
---------------
|  si         |
---------------
|  sj         |
---------------

有人可以解释一下我在这里想念的吗?

Can somebody please explain what I am missing here?

推荐答案

执行此操作时:

struct s myStruct;
myStruct.si = 9;

您不是正在初始化 myStruct.您在没有初始化程序的情况下声明 ,然后运行一条语句来设置一个字段.

You are not initializing myStruct. You declare it without an initializer, then run a statement to set one field.

由于该变量未初始化,因此其内容未定义,并且读取该变量为未定义行为.这意味着看似无关的更改可以修改此行为.在您的示例中,添加了一个额外的变量 happened ,导致myStruct.sj为0,但不能保证会如此.

Because the variable is uninitialized, its contents are undefined, and reading it is undefined behavior. This means that seemingly unrelated changes can modify this behavior. In your example adding an extra variable happened to cause myStruct.sj to be 0, but there's no guarantee that this will be the case.

初始化变量,必须在定义时为其赋予一个值:

To initialize a variable, you have to give it a value at the time it is defined:

struct s myStuct = { 9 };

执行此操作,然后您会看到myStruct.sj的内容设置为0.根据

One you do this, then you'll see the contents of myStruct.sj set to 0. This is guaranteed as per section 6.7.8 of the C standard (with highlighting specific to this case):

10如果具有自动存储持续时间的对象不是 显式初始化,其值不确定. 如果有物体 具有静态存储持续时间的未初始化 明确,然后:

10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

-如果具有指针类型,则将其初始化为null 指针;

—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.

...

21 如果用大括号括起来的列表中的初始化程序比那里少 是集合的元素或成员,或者 用于初始化大小已知的数组的字符串文字 是数组中的元素,其余部分 应该与具有静态对象的对象隐式地初始化 存储期限.

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.

这篇关于部分初始化C结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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