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. poj1185:炮兵阵地(状压dp)

    也算是比较基础的状压dp了,跟做过的第二道比较又稍微复杂了一点 需要记录之前两行的状态.. 统计结果也稍有不同 另外还学习了一个得到一个整数二进制位 1 的个数的位运算方法 详见代码: #includ ...

  2. Kuhn-Munkres算法。带权二分图匹配模板 (bin神小改版本)

    /****************************************************** 二分图最佳匹配 (kuhn munkras 算法 O(m*m*n)). 邻接矩阵形式 . ...

  3. 打开首页老是加载themes.googleusercontent.com_Wopus问答

    打开首页老是加载themes.googleusercontent.com_Wopus问答 打开首页老是加载themes.googleusercontent.com

  4. Activity小结

    Log日志类的五种级别 1.由高到低分别是:v.i.d.w.e 2.生命周期有七种状态: onCreate:创建 onStart:启动 onResume:显示(可以与用户交互) onPause:暂停 ...

  5. 第35讲 Activity入门和跳转

    第35讲Activity入门和跳转 1.Activity Activity是用户接口程序.在Android当中,Activity提供可视化的用户界面,一个Android应用通常由多个activity组 ...

  6. JavaScript 数组中查找符合条件的值

    数组实例的find方法,用于找出第一个符合条件的数组成员.它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该成员.如果没有符合条件的成员,则返回u ...

  7. 【CSS3】横屏引导小动画

    演示地址:http://codepen.io/anon/pen/oXbXdX 主要知识点: @media all and (orientation : landscape) { /* 这是匹配横屏的状 ...

  8. 用户控件(.ascx)与<ul><li>以及<a>布局之小结

    用户控件(.ascx)与<ul><li>以及<a>布局 小结 一.用户控件(.ascx) 1.aspx是浏览器直接访问的页面,而ascx是用户控件,一般是用来重用. ...

  9. Spring3 MVC 之 Hello Word

    开发工具: MyEclipse  10.0 项目目录: [http://www.cnblogs.com/rhythmK/] 1.新建项目:File->New->Web Project  项 ...

  10. JAVA学习第三十课(经常使用对象API)- String类:类方法练习

    intern方法 public class Main { public static void main(String[] args) { String str1 = new String(" ...