Transferred from: http://hnniyan123.blog.chinaunix.net/uid-29917301-id-4989879.html
In the Linux2.6 version of the kernel, we can often see the definition and initialization of the following struct body. This is rarely seen in the previous C language book. The following structure comes from a part of the Linux kernel. In this struct we can see that there are normal integer variables and pointers to functions.
struct net_proto_family { int family; int (*create) (structstruct socket *sock, intint kern); struct Module *owner;};
In the kernel, the following methods can be used for initialization.
Static Const struct net_proto_family netlink_family_ops = { = Pf_netlink, = netlink_create , . Owner = this_module,/ **/};
Here is a small program that uses the structure above, and there is another application in the structure:
#include <stdio.h>#definePF_ID 10enum{ID1=Ten, ID2, ID3,};Char*message ="message";intCreateintFdChar*name) { if(FD = =Ten) {printf ("I am Create fd =%d,name =%s\n", Fd,name); } return 1;}intOutput (intFdChar*name) {printf ("I am Output fd =%d,name =%s\n", Fd,name); return 1;}intOutput_2 (intFdChar*name) {printf ("I am output_2 fd =%d,name =%s\n", Fd,name);}structtest_1{int(*output) (intFdChar*name);};structtest{intID; Charname[ -]; int(*print) (intFdChar*name);};Static structTest des ={. ID=pf_id,. Name="Frank",. Print=Create,};structTest_1 table[] ={[ID1]= {. Output =output}, [ID2]= {. Output =output_2},};intMain () {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");}
Here is an article that analyzes the data structure of the Linux kernel in more detail.
Http://blog.csdn.net/mociml/archive/2009/08/13/4443280.aspx
Linux2.6 initialization of structures in kernel (reprint)