“struct a a1 = {0};”与“struct a a2 = {5}不同”;“为什么?(“struct a a1 = {0};” different from “struct a a2 =

编程入门 行业动态 更新时间:2024-10-08 22:49:23
struct a a1 = {0};”与“struct a a2 = {5}不同”;“为什么?(“struct a a1 = {0};” different from “struct a a2 = {5};” why?)

如果struct a a1 = {0}; 将一个结构的所有元素(不同类型)初始化为零,然后构造struct a a2 = {5}; 应该初始化为5 ..不是?

#include <stdio.h> typedef struct _a { int i; int j; int k; }a; int main(void) { a a0; a a1 = {0}; a a2 = {5}; printf("a0.i = %d \n", a0.i); printf("a0.j = %d \n", a0.j); printf("a0.k = %d \n", a0.k); printf("a1.i = %d \n", a1.i); printf("a1.j = %d \n", a1.j); printf("a1.k = %d \n", a1.k); printf("a2.i = %d \n", a2.i); printf("a2.j = %d \n", a2.j); printf("a2.k = %d \n", a2.k); return 0; }

未初始化的结构包含垃圾值

a0.i = 134513937 a0.j = 134513456 a0.k = 0

初始化为0结构包含所有初始化为0元素

a1.i = 0 a1.j = 0 a1.k = 0

初始化为5结构只包含初始化为5的第一个元素,其余元素初始化为0 。

a2.i = 5 a2.j = 0 a2.k = 0

a2.j和a2.k总是保证在a a2 = {5};期间初始化为0 a a2 = {5}; (或)是一种undefined behavior

OTOH,为什么我没有看到s2所有元素都初始化为5 。 如何在{0}期间完成struct初始化,以及在使用{5}时它有什么不同?

If struct a a1 = {0}; initializes all the elements (of different types) of a structure to zero, then struct a a2 = {5}; should initialize it to 5.. no?

#include <stdio.h> typedef struct _a { int i; int j; int k; }a; int main(void) { a a0; a a1 = {0}; a a2 = {5}; printf("a0.i = %d \n", a0.i); printf("a0.j = %d \n", a0.j); printf("a0.k = %d \n", a0.k); printf("a1.i = %d \n", a1.i); printf("a1.j = %d \n", a1.j); printf("a1.k = %d \n", a1.k); printf("a2.i = %d \n", a2.i); printf("a2.j = %d \n", a2.j); printf("a2.k = %d \n", a2.k); return 0; }

The uninitialized struct contains garbage values

a0.i = 134513937 a0.j = 134513456 a0.k = 0

The initialized to 0 struct contains all elements initialized to 0

a1.i = 0 a1.j = 0 a1.k = 0

The initialized to 5 struct contains only the first element initialized to 5 and the rest of the elements initialized to 0.

a2.i = 5 a2.j = 0 a2.k = 0

Would a2.j and a2.k always guaranteed to initialize to 0 during a a2 = {5}; (or) is it an undefined behavior

OTOH, why am I not seeing all the elements of s2 initialized to 5. How is the struct initialization is done during {0} and how is it different when {5} is used?

最满意答案

参考:

C99标准6.7.8.21

如果大括号包含的列表中的初始值设定项少于聚合的元素或成员,或者用于初始化已知大小的数组(而不是数组中的元素)的字符串文字中的较少字符,则聚合的其余部分应被隐式地初始化为具有静态存储持续时间的对象。

[编辑]

静态对象和隐式初始化:

对象的存储持续时间决定了对象的生存期。 有3个存储时间: 静态,自动和分配

在所有块之外声明的变量以及使用静态存储类说明符显式声明的变量具有静态存储持续时间 。 编译器默认将静态变量初始化为零。

考虑以下程序:

#include<stdio.h> int main() { int i; static int j; printf("i = [%d]",i); printf("j = [%d]",j); return 0; }

在上面的程序中, i有自动存储,并且由于它没有明确初始化,所以它的值是Undefined 。 虽然j具有静态存储持续时间,并且保证由编译器将其初始化为0 。

Reference:

C99 Standard 6.7.8.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.

[EDIT]

Static objects and implicit initialization:

The storage duration of an object determines the lifetime of an object. There are 3 storage durations: static, automatic, and allocated

variables declared outside of all blocks and those explicitly declared with the static storage class specifier have static storage duration. Static variables are initialized to zero by default by the compiler.

Consider the following program:

#include<stdio.h> int main() { int i; static int j; printf("i = [%d]",i); printf("j = [%d]",j); return 0; }

In the above program, i has automatic storage and since it is not explicitly initialized its value is Undefined. While j has static storage duration and it is guaranteed to be initialized to 0 by the compiler.

更多推荐

本文发布于:2023-07-19 23:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1188600.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:struct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!