转自:http://hnniyan123.blog.chinaunix.net/uid-29917301-id-4989879.html

在Linux2.6版本的内核中,我们经常可以看到下面的结构体的定义和初始化。这在以前的C语言书中是极少见到的。下面的一个结构体来自到Linux内核中的一部分。在这个结构体中我们可以看到有普通的整型变量,也有函数的指针。

struct net_proto_family {
int family;
int (*create)(struct net *net, struct socket *sock,
int protocol, int kern);
struct module *owner;
};

而在内核中可以使用下面的方法进行初始化。

static const struct net_proto_family netlink_family_ops = {
.family = PF_NETLINK,
.create = netlink_create,
.owner = THIS_MODULE, /* for consistency 8) */
};

下面是使用上面的结构体的一个小程序,其中还有结构体中的另外一种应用:

#include <stdio.h>
#define PF_ID 10 enum
{
ID1 = ,
ID2,
ID3,
}; char *message = "message";
int create(int fd,char *name)
{
if(fd == )
{
printf ("I am create fd = %d,name = %s\n",fd,name);
}
return ;
} int output (int fd,char *name)
{
printf("I am output fd =%d,name = %s\n",fd,name);
return ;
} int output_2 (int fd,char *name)
{
printf("I am output_2 fd = %d,name = %s\n",fd,name);
} struct test_1
{
int (*output)(int fd,char *name);
}; struct test
{
int id;
char name[];
int (*print)(int fd,char *name);
}; static struct test des ={ .id= PF_ID,
.name ="frank",
.print = create,
}; struct test_1 table[] = {
[ID1] = {.output = output},
[ID2] = {.output = output_2},
}; int main()
{
printf("des.PF_ID=%d\n",des.id);
printf("des.message= %s\n",des.name);
des.print(PF_ID,message);
table[ID2].output(PF_ID,"table ID2");
table[ID1].output(PF_ID,"table ID1");
}

这里有一篇分析Linux内核这种数据结构比较详细的文章。
http://blog.csdn.net/mociml/archive/2009/08/13/4443280.aspx

Linux2.6 内核中结构体初始化(转载)的更多相关文章

  1. Linux C中结构体初始化

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

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

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

  3. 关于c语言中结构体的初始化

    1.先定义结构体类型后再定义结构体变量: 格式为:struct 结构体名 变量名列表: struct book s1,s2,*ss://注意这种之前要先定义结构体类型后再定义变量: 2.在定义结构体类 ...

  4. C语言中结构体赋值问题的讨论(转载)

    今天帮师姐调一个程序的BUG,师姐的程序中有个结构体直接赋值的语句,在我印象中结构体好像是不能直接赋值的,正如数组不能直接赋值那样,我怀疑这个地方有问题,但最后证明并不是这个问题.那么就总结一下C语言 ...

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

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

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

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

  7. Linux下C结构体初始化

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

  8. Linux内核kobject结构体分析

    1.前言 Linux内核中有大量的驱动,而这些驱动往往具有类似的结构,根据面向对象的思想,可以将共同的部分提取为父类,而这个父类就是kobject,kobject结构体中包含了大量设备的必须信息,而三 ...

  9. Go语言中结构体的使用-第2部分OOP

    1 概述 结构体的基本语法请参见:Go语言中结构体的使用-第1部分结构体.结构体除了是一个复合数据之外,还用来做面向对象编程.Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. ...

随机推荐

  1. GoogLeNet系列解读

    GoogLeNet Incepetion V1 这是GoogLeNet的最早版本,出现在2014年的<Going deeper with convolutions>.之所以名为“GoogL ...

  2. 【转】fedora和ubuntu开启ssh

    fedora和ubuntu开启ssh 1小时前 ubuntu开启SSH服务 SSH分客户端openssh-client和openssh-server如果你只是想登陆别的机器的SSH只需要安装opens ...

  3. WSDL4J解析WSDL文件方法

    利用wsdl4j解析WSDL文件 工具:wsdl4j1.6 解析wsdl文件是axis1.4的服务wsdl文件 wsdl文件: <?xml version="1.0" enc ...

  4. [HTML5] Show Images of Differing Resolutions Depending on the Viewport Width with srcset

    For small viewports, we want to save bandwidth and we may be dealing with slow speeds; so it's very ...

  5. android_checkbox_dialog 设计 是不是要开起 默认不提示对话框

    android_checkbox_dialog设计是不是开起默认不提示 package com.example.android_checkbox_dialog; import android.app. ...

  6. C# 语言历史版本特性(C# 1.0到C# 7.1汇总更新) C#各版本新特性 C#版本和.NET版本以及VS版本的对应关系

    C# 语言历史版本特性(C# 1.0到C# 7.1汇总更新) 2017年08月06日 11:53:13 阅读数:6705 历史版本 C#作为微软2000年以后.NET平台开发的当家语言,发展至今具有1 ...

  7. Android--向SD卡读写数据

    // 向SD卡写入数据 private void writeSDcard(String str) { try { // 推断是否存在SD卡 if (Environment.getExternalSto ...

  8. Quick-Cocos2d3.2RC1在Code IDE中实现代码提示

    之前写Lua最痛苦的就是代码提示问题,如今官方给了IDE很好用.以下说Quick使用IDE加入代码提示问题. 第一步:制作api提示压缩包. 须要使用控制台实现方法例如以下: 1.找到framewor ...

  9. Sublime Text2-Control Package---ShinePans

    1.打开sublime Text2 2.菜单条中的preference>>BrowsePackages 3.退到上一级打开Installed Packages 4.拷贝文件到此目录 (Pa ...

  10. Creating a .bash_profile on your mac

    A typical install of OS X won't create a .bash_profile for you. When you want to run functions from ...