Linux 内核中用到了大量的结构体,在编码规范中也给出了结构体初始化的规则,这篇文章中有对其的解释:http://blog.csdn.net/dlutbrucezhang/article/details/10296897,不过,这篇文章中并没有给出实例分析,下面我写了一段测试程序:

#include<stdio.h>
#include<string.h> struct test {
int test_value1;
float test_value2;
char *test_value3;
}; int main(void)
{
int i;
char my_name[] = "DLUTBruceZhang";
char my_school[] = "DLUT"; for(i = 0; i < 2; i++){
if (i % 2 == 0){
struct test my_test = {
.test_value1 = 10,
.test_value2 = 10.0,
.test_value3 = my_name,
};
printf("test_value1 = %d, test_value2 = %f,\
test_value3 = %s\n", my_test.test_value1,
my_test.test_value2, my_test.test_value3); } else {
struct test my_test = {
.test_value1 = 100,
.test_value2 = 100.0,
.test_value3 = my_school,
};
printf("test_value1 = %d, test_value2 = %f,\
test_value3 = %s\n", my_test.test_value1,
my_test.test_value2, my_test.test_value3);
}
}
struct test my_test = {
/*.test_value1 = 10,*/
/*.test_value2 = 10.0,*/
/*.test_value3 = my_name,*/
};
printf("test_value1 = %d, test_value2 = %f,\
test_value3 = %s\n", my_test.test_value1,
my_test.test_value2, my_test.test_value3); return 0;
}

分析:

1.首先给出结构体的定义,它包含三个字段,一个整型,一个浮点型,一个字符指针

      struct test {
int test_value1;
float test_value2;
char  *test_value3;
      };
2.两次赋初值,根据情况不同,对其进行赋值,赋值方法采用的是 Linux 内核编码规范中的方法(这里忽略了标识符)

struct test my_test = {
.test_value1 = 10,
.test_value2 = 10.0,
.test_value3 = my_name,
};

struct test my_test = {
.test_value1 = 100,
.test_value2 = 100.0,
.test_value3 = my_school,
};

3.不赋初值的情况是整型默认为 0,浮点型默认为 0.0,字符指针默认为 NULL

struct test my_test = {
/*.test_value1 = 10,*/
/*.test_value2 = 10.0,*/
/*.test_value3 = my_name,*/
};

      下面,运行这个测试程序,验证上述说法:


Linux2.6内核 -- 结构的初始化的更多相关文章

  1. Linux C中结构体初始化

          在阅读GNU/Linux内核代码时,我们会遇到一种特殊的结构初始化方式.该方式是某些C教材(如谭二版.K&R二版)中没有介绍过的.这种方式称为指定初始化(designated in ...

  2. Linux2.6内核实现的是NPTL

    NPTL是一个1×1的线程模型,即一个线程对于一个操作系统的调度进程,优点是非常简单.而其他一些操作系统比如Solaris则是MxN的,M对应创建的线程数,N对应操作系统可以运行的实体.(N<M ...

  3. 【转载】linux2.6内核initrd机制解析

    题记 很久之前就分析过这部分内容,但是那个时候不够深入,姑且知道这么个东西存在,到底怎么用,来龙去脉咋回事就不知道了.前段时间工作上遇到了一个initrd的问题,没办法只能再去研究研究,还好,有点眉目 ...

  4. Linux2.6 内核的 Initrd 机制解析

    文章来自:www.ibm.com/developerworks/cn/linux/l-k26initrd/ 1.什么是 Initrd initrd 的英文含义是 boot loader initial ...

  5. Linux2.6 内核的 Initrd 机制解析(转)

    from: https://www.ibm.com/developerworks/cn/linux/l-k26initrd/ 简介: Linux 的 initrd 技术是一个非常普遍使用的机制,lin ...

  6. Linux C 结构体初始化三种形式

    最近看linux代码时发现了结构体 struct 一种新的初始化方式,各方查找对比后总结如下: 1. 顺序初始化教科书上讲C语言结构体初始化是按照顺序方式来讲的,没有涉及到乱序的方式.顺序初始化str ...

  7. Linux下C结构体初始化[总结]

    1.前言 今天在公司看一同事写的代码,代码中用到了struct,初始化一个struct用的是乱序格式,如下代码所示: typedef struct _data_t { int a; int b; }d ...

  8. Linux下C结构体初始化

    1.前言 今天在公司看一同事写的代码,代码中用到了struct,初始化一个struct用的是乱序格式,如下代码所示: typedef struct _data_t { int a; int b; }d ...

  9. DDR3内存详解,存储器结构+时序+初始化过程

    DDR3内存详解,存储器结构+时序+初始化过程 标签: DDR3存储器博客 2017-06-17 16:10 1943人阅读 评论(1) 收藏 举报  分类: 硬件开发基础(2)  转自:http:/ ...

随机推荐

  1. hdu 4055 Number String(dp)

    Problem Description The signature of a permutation is a string that is computed as follows: for each ...

  2. (转)iOS7界面设计规范(3) - UI基础 - 启动与退出

    周二晚间来第三发,搞得好像今天是周六的赶脚.发掉之后再奖励自己一点冰啤酒吧,然后扑床去.天气热起来了,各位注意防暑降温呗.走起. 重要:这是针对于正在开发中的API或技术的预备文档(预发布版本).虽然 ...

  3. jquery Tabs选项卡切换

    效果: HTML部分: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  4. 为人们服务的asp.net 验证控件

    ASP.NET是微软推出的WEB开发工具,他有很强大的功能,今天看视频讲到验证控件这一部分,真的感受到了微软全心全意为人民服务了.越来越佩服微软了,人家都设计出来了,咱们一定要会用才可以啊,不然太…. ...

  5. mysql高可用方案MHA介绍

    mysql高可用方案MHA介绍 概述 MHA是一位日本MySQL大牛用Perl写的一套MySQL故障切换方案,来保证数据库系统的高可用.在宕机的时间内(通常10-30秒内),完成故障切换,部署MHA, ...

  6. .NET技术

    1.在C#中,string str = null 与 string str = “” 请尽量使用文字或图象说明其中的区别.回答要点:说明详细的空间分配.(10分) 解:string str=null ...

  7. centos打开3306端口

    centos默认是关闭了3306端口的,外网通过3306端口不能访问数据库,这时需呀打开3306端口1.打开端口: /sbin/iptables -I INPUT -p tcp --dport 330 ...

  8. easyUI的doCellTip 就是鼠标放到单元格上有个提示的功能

    1:这个东西是我抄的(抄的哪儿的我就想不起来了- -)弹出的窗没有样式  不是很好看 //扩展 $.extend($.fn.datagrid.methods, { /** * 开打提示功能 * @pa ...

  9. mysql配置文件my.cnf解析转载

    basedir = path 使用给定目录作为根目录(安装目录). character-sets-dir = path 给出存放着字符集的目录. datadir = path 从给定目录读取数据库文件 ...

  10. Oracle复杂查询

    1:列出所有员工的姓名,部门名称,和工资 select a1.ename,a1.sal,a2.dname from emp a1,dept a2 where a1.deptno = a2.deptno ...