结构程序说明

Explanation of structures program

我遇到了这段代码:

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

struct gospel
{
    int num;
    char mess1[50];
    char mess2[50];
} m1 = { 2, "If you are driven by success",
"make sure that it is a quality drive"
};

int main()
{
        struct gospel m2, m3;
        m2 = m1;
        m3 = m2;
        printf("\n%d %s %s", m1.num, m2.mess1, m3.mess2);

    return 0;
}

但是我无法得到 m1 =

行的内容
{ 2, "If you are driven by success",
"make sure that it is a quality drive"
};

是什么意思?有人可以解释它的含义以及这个程序的输出吗?

struct gospel
{
    int num;
    char mess1[50];
    char mess2[50];
} m1 = { 2, "If you are driven by success",
"make sure that it is a quality drive" };

正在定义类型为 struct gospel

的全局变量

写的一样

struct gospel
{
    int num;
    char mess1[50];
    char mess2[50];
};

struct gospel m1 = { 2, "If you are driven by success",
"make sure that it is a quality drive" };

大括号按顺序将结构的值分配给变量 - 因此 m1.num 被分配了值 2,m1.mess1 被分配了值“如果你被成功驱动”和 m1。 mess2 被分配了值“确保它是一个质量驱动器”

如果您想了解有关此类初始化的更多信息,建议阅读 this article