再看linux kernel代码的时候发现的东东
static struct inet_protocol tcp_protocol = {
handler: tcp_v4_rcv,
err_handler: tcp_v4_err,
next: IPPROTO_PREVIOUS,
protocol: IPPROTO_TCP,
name: "TCP"
};
开始不晓得啥意思,原来是这样的。
struct inet_protocol
{
int (*handler)(struct sk_buff *skb);
void (*err_handler)(struct sk_buff *skb, u32 info);
struct inet_protocol *next;
unsigned char protocol;
unsigned char copy:1;
void *data;
const char *name;
};
原来意思是将handler赋值成tcp_v4_rcv, err_hander赋值成tcp_v4_err, 以此类推。
今天又发现一种赋值的形式:
static struct inet_protocol tcp_protocol = {
.handler= tcp_v4_rcv,
.err_handler= tcp_v4_err,
.next= IPPROTO_PREVIOUS,
.protocol= IPPROTO_TCP,
.name= "TCP"
};
前面加个点,后面加个等号,这样都可以。
下面是一个获得结构体偏移的方法。
#include <stdio.h>
struct AA
{
int a;
char b;
}B,C;
int main()
{
B.a = 4;
B.b = 'a';
printf("%p/n", &B);
int pB = (int)&B; //这时 pB是B结构体的地址
printf("%x/n", pB);
// 这样是获得B结构体中的内容
printf("B.a is %d, B.b is %c/n", ((struct AA*)(pB))->a, ((struct AA*)(pB))->b );
// 用一个特殊的0作为 结构AA的基地址, 此时在取某个成员的地址,可以得到这个成员在结构中
// 的偏移
printf("offset of a is %d/n", &(((struct AA*)(0))->b) );
return 0;
}
输出内容:
hman@linux-plog:~> ./a.out
0x804a01c
804a01c
B.a is 4, B.b is a
offset of a is 4