直接上示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
 
struct student_st
{
    char c;
    int score;
    const char *name;
};
 
static void show_student(struct student_st *stu)
{
    printf("c = %c, score = %d, name = %s\n", stu->c, stu->score, stu->name);
}
 
int main(void)
{
    // method 1: 按照成员声明的顺序初始化
    struct student_st s1 = {'A', 91, "Alan"};
    show_student(&s1);
 
    // method 2: 指定初始化,成员顺序可以不定,Linux 内核多采用此方式
    struct student_st s2 =
    {
        .name = "YunYun",
        .c = 'B',
        .score = 92,
    };
    show_student(&s2);
 
    // method 3: 指定初始化,成员顺序可以不定
    struct student_st s3 =
    {
        c: 'C',
        score: 93,
        name: "Wood",
    };
    show_student(&s3);
     
    return 0;
}</stdio.h>

运行结果

如果想初始化结构体数组,可采用 {{ }, { }, { }} 方式,如

1
2
3
4
5
6
7
8
9
10
11
12
13
struct student_st stus[2] =
{
    {
        .c = 'D',
        .score = 94,
        /*也可以只初始化部分成员*/
    },
    {
        .c = 'D',
        .score = 94,
        .name = "Xxx"
    },
};

C语言结构体初始化的三种方法的更多相关文章

  1. C语言结构体初始化的四种方法(转载)

    原文:https://blog.csdn.net/ericbar/article/details/79567108 定义 struct InitMember { int first: double s ...

  2. C语言结构体初始化的四种方法

    定义 struct InitMember{    int first:    double second:    char* third:    float four;}; 方法一:定义时赋值 str ...

  3. C语言结构体定义的几种方法

    什么是结构体? 在C语言中,结构体(struct)指的是一种数据结构,是C语言中聚合数据类型(aggregate data type)的一类.结构体可以被声明为变量.指针或数组等,用以实现较复杂的数据 ...

  4. c 语言结构体struct的三种定义方式 及 typedef

    struct 结构体名{ 成员列表: ..... }结构体变量: 结构体类型变量的定义 结构体类型变量的定义与其它类型的变量的定义是一样的,但由于结构体类型需要针对问题事先自行定义,所以结构体类型变量 ...

  5. js二维数组定义和初始化的三种方法总结

    js二维数组定义和初始化的三种方法总结 方法一:直接定义并且初始化,这种遇到数量少的情况可以用var _TheArray = [["0-1","0-2"],[& ...

  6. C语言结构体初始化方法

    早上苏凯童鞋问我这个问题来着,写在这里. 我了解到的C中结构体初始化的方法大概有三种. 如这里我定义了一个结构体: typedef struct node { int x, y; }Node; 第一种 ...

  7. [C语言]结构体初始化的不同方法

    结构体的定义 struct Test{ int first; double second; }Sample; 方法一 定义时直接赋值 Sample s(1, 1.00); 缺点:必须匹配变量顺序,不能 ...

  8. C语言结构体初始化(转载)

    <代码大全>建议在变量定义的时候进行初始化,但是很多人,特别是新人对结构体或者结构体数组定义是一般不会初始化,或者不知道怎么初始化. 1.初始化 typedef struct _TEST_ ...

  9. c语言结构体定义的几种形式

    转自https://blog.csdn.net/ziguo2010/article/details/79897327 1.最常用定义方式:定义结构体data,此时结构体相当于一个类型,比如int,如需 ...

随机推荐

  1. 2154: Crash的数字表格

    2154: Crash的数字表格 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 3372  Solved: 1258[Submit][Status][ ...

  2. 02、微信小程序的数据绑定

    02.微信小程序的数据绑定 目录结构: 模板内容: 使用bindtap绑定事件 <!--index.wxml--> <view class="container" ...

  3. Career Planning:Developers Best Practices Tutorial

    This small tutorial is based on my past 16+ years of experience in software development industry. I ...

  4. 关于Nginx部署Django项目的资料收集

    参考:https://www.cnblogs.com/chenice/p/6921727.html 参考:https://blog.csdn.net/fengzq15/article/details/ ...

  5. django博客项目7

    ................

  6. select * 为什么不好? limit 1 为什么好? --mysql SQL语句优化

    问题一: Select * from  student;  这种语句不好 我的理解:根据Innode存储引擎以及网上的各种资料所说的innodb的B+树索引结构可以分析出,当在非聚集索引列上搜索若用s ...

  7. 【转】记一次SQLServer的分页优化兼谈谈使用Row_Number()分页存在的问题

      最近有项目反应,在服务器CPU使用较高的时候,我们的事件查询页面非常的慢,查询几条记录竟然要4分钟甚至更长,而且在翻第二页的时候也是要这么多的时间,这肯定是不能接受的,也是让现场用SQLServe ...

  8. use html5 video tag with MSE for h264 live streaming

    本编博客记录桌面虚拟化移动端预研. 完整demo: https://github.com/MarkRepo/wfs.js 常见的直播方案有RTMP RTSP HLS 等等, 由于这些流都需要先传输到服 ...

  9. win7开启特定端口

    win7开启特定端口        在xp系统的时代,修改防火墙很方便,很简单.windows7或许是做得过于复杂了.当然所谓安全性也是相当于其他之前版本的系统更高了.为什么要打开端口,肯定是在win ...

  10. 什么是web接口

    当我们在请求一个页面的时候,会显示服务器返回的资源,其中包含了HTML.CSS和JS,除此之外,服务器还可以返回图片.视频.字体和插件等类型的资源.这些资源全部由HTTP协议传输. 如果把HTTP协议 ...