简体   繁体   English

struct c 中的默认值 [重复]

[英]Default values in struct c [duplicates]

What does the code below print when running the function print_test()?运行函数print_test() 时,下面的代码打印什么?

struct test {
  int a, b, c;
};

void print_test(void) {
  struct test t = {1};
  printf("%d/%d/%d", t.a, t.b, t.c);
}

Solution 1\\0\\0解决方案 1\\0\\0

Why are b and c initialized to zero even though I did not do it?为什么 b 和 c 初始化为零,即使我没有这样做? Are there any default values for the struct?该结构是否有任何默认值? However, the struct is not global and the member is not static.但是,结构不是全局的,成员也不是静态的。 Why are they automatically zero-initialized?为什么它们会自动零初始化? And if the data type were not int another data type to which value will be initialized?如果数据类型不是 int 另一个数据类型将被初始化为哪个值?

If you don't specify enough initializers for all members of a struct, you'll face Zero initialization , which will initialize remaining members to 0 .如果您没有为结构的所有成员指定足够的初始化程序,您将面临 零初始化,这会将其余成员初始化为0 I think by today's standards this seems a bit odd, especially because C++'s initialization syntax has evolved and matured a lot over the years.我认为按照今天的标准,这似乎有点奇怪,尤其是因为 C++ 的初始化语法多年来已经发展和成熟了很多。 But this behavior remains for backwards-compatibility.但是这种行为仍然是为了向后兼容。

I think we need to know two points at this stage:我觉得现阶段我们需要知道两点:

  1. There is nothing different between regular variables and structs, if they are at local scope ie automatic storage duration .常规变量和结构之间没有什么不同,如果它们在本地范围内,即automatic storage duration They will contain garbage values.它们将包含垃圾值。 Using those values could invoke undefined behaviour.使用这些值可能会调用未定义的行为。

The only thing that makes structs different is that if you initialise at least one of the members, the rest of the members will get set to zero ie initialised as if they had static storage duration .唯一使结构不同的是,如果您至少初始化其中一个成员,则其余成员将被设置为零,即初始化为好像它们具有static storage duration But that's not the case when none of the members are initialised.但是当没有任何成员被初始化时,情况就不是这样了。

  1. It depends on your declaration.这取决于您的声明。 If your declaration is outside any function or with the static keyword (more precisely, has static storage duration ), the initial value of x is a null pointer (which may be written either as 0 or as NULL).如果您的声明在任何函数之外或使用static keyword (更准确地说,具有static storage duration ),则 x 的初始值是一个null pointer (可以写为 0 或 NULL)。

If it's inside a function ie it has automatic storage duration , its initial value is random (garbage).如果它在一个函数内,即它有automatic storage duration ,它的初始值是随机的(垃圾)。

consider the following code:考虑以下代码:

#include<stdio.h>
#include<unistd.h>

struct point {
    int x, y;
    char a;
    double d;
};    
typedef struct point Point;

void main(){ 
    Point p1;
    printf("\nP1.x: %d\n", p1.x);
    printf("\nP1.y: %d\n", p1.y);
    printf("\nP1.a: %d\n", p1.a);
    printf("\nP1.d: %lf\n", p1.d);

    Point p2 = {1};
    printf("\nP2.x: %d\n", p2.x);
    printf("\nP2.y: %d\n", p2.y);
    printf("\nP2.a: %d\n", p2.a);
    printf("\nP2.d: %lf\n", p2.d);
}

The output is :输出是:

P1.x: 0

P1.y: 66900

P1.a: 140

P1.d: 0.000000

P2.x: 1

P2.y: 0

P2.a: 0

P2.d: 0.000000

A Good read: C and C++ : Partial initialization of automatic structure好读物: C 和 C++:自动结构的部分初始化

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

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