list_head实践
''~``
( o o )
+------------------.oooO--(_)--Oooo.---------------------+
| Rick Wang |
| E-mail: rikyok@gmail.com |
| XIDIAN University http://www.xdwy.com.cn |
| Xi'an, China ( ) |
+---------------------\ (----( )-----------------------+
\_) ) /
(_/
( o o )
+------------------.oooO--(_)--Oooo.---------------------+
| Rick Wang |
| E-mail: rikyok@gmail.com |
| XIDIAN University http://www.xdwy.com.cn |
| Xi'an, China ( ) |
+---------------------\ (----( )-----------------------+
\_) ) /
(_/
1
2
#include <stdio.h>
3
#include "list.h"
4
5
struct test{
6
int a;
7
char b;
8
};
9
struct ts_hlist{
10
struct list_head member1;
11
int member2;
12
char member3;
13
double member4;
14
};
15
16
LIST_HEAD(hellolist);
17
struct ts_hlist hellots;
18
struct ts_hlist * rs;
19
int main(void){
20
21
struct test t1;
22
t1.a=8;
23
t1.b='z';
24
char *pchar="testok";
25
int i=0;
26
printf("length of int:%d char:%d double:%d long:%d\n",sizeof(int),sizeof(char),sizeof(double),sizeof(long));
27
printf("Address of t1 is:%p\nddd member of a is:%d\n member of b is:%c\n",&t1,t1.a,t1.b);
28
printf("Address of pchar is:%p\nvalue of pchar is:%s\n",pchar,pchar);
29
for(;i<strlen(pchar);i++)
30
{
31
printf("Address of member of pchar in pos %d is %p ,value of pchar in pos%d is:%c\n",i,pchar+i,i,pchar[i]);
32
}
33
printf("address base=0\n,than the address of a:%d\naddress of b:%d\n", &((struct test *)0)->a,&((struct test *)0)->b) ;
34
35
36
printf("If the NULL struct as 0 of ts_hlist is given,then the size of 0based willbe:like this:\n\n\n\n");
37
printf(" struct ts_hlist as NULL \n");
38
printf("%u --->|__________|\n",&( ((struct ts_hlist *)0)->member1) );
39
printf(" |__________|\n");
40
printf(" |__________|\n");
41
printf(" |__________|\n");
42
printf(" |__________|\n");
43
printf(" |__________|\n");
44
printf(" |__________|\n");
45
printf(" |__________|\n");
46
printf("%u --->|__________|\n",&( ((struct ts_hlist *)0)->member2) );
47
printf(" |__________|\n");
48
printf(" |__________|\n");
49
printf(" |__________|\n");
50
printf("%u--->|__________|\n",&( ((struct ts_hlist *)0)->member3) );
51
printf(" |__________|\n");
52
printf(" |__________|\n");
53
printf(" |__________|\n");
54
printf("%u--->|__________|\n",&( ((struct ts_hlist *)0)->member4) );
55
printf(" |__________|\n");
56
printf(" |__________|\n");
57
printf(" |__________|\n");
58
printf(" |__________|\n");
59
printf(" |__________|\n");
60
printf(" |__________|\n");
61
printf(" |__________|\n");
62
63
printf(" \n\n");
64
printf(" struct ts_hlist as allocate space\n");
65
int j=0;
66
67
printf(" size of ts_hlist struct is %d\n",sizeof(struct ts_hlist));
68
for(;j<sizeof(struct ts_hlist);j++)
69
{
70
printf(" |__________|<--%p\n",&hellots+(unsigned long)j);
71
}
72
printf(" the address of head(type of head_list) is:%p\n",hellolist);
73
74
hellots.member2=3;
75
hellots.member3='A';
76
hellots.member4=44.33;
77
list_add_tail(&hellots.member1,&hellolist);
78
rs=list_entry(&hellots.member1,struct ts_hlist,member1);
79
printf(" the address of member1 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member1) );
80
printf(" the address of member2 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member2) );
81
printf(" the address of member3 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member3) );
82
printf(" the address of member4 is: %p\n",&hellots+(unsigned long)&( ((struct ts_hlist *)0)->member4) );
83
printf(" Now we'll test the value area of ts_hlist>>>>>\n");
84
printf(" member2 of rs is:%d\n", rs->member2);
85
printf(" member3 of rs is:%c\n", rs->member3);
86
printf(" member4 of rs is:%g\n", rs->member4);
87
}
88
89
90
Result:
91
92
93
If the NULL struct as 0 of ts_hlist is given,then the size of 0based willbe:like this:
94
95
96
struct ts_hlist as NULL
97
0 --->|__________|
98
|__________|
99
|__________|
100
|__________|
101
|__________|
102
|__________|
103
|__________|
104
|__________|
105
8 --->|__________|
106
|__________|
107
|__________|
108
|__________|
109
12--->|__________|
110
|__________|
111
|__________|
112
|__________|
113
16--->|__________|
114
|__________|
115
|__________|
116
|__________|
117
|__________|
118
|__________|
119
|__________|
120
|__________|
121
122
123
struct ts_hlist as allocate space
124
size of ts_hlist struct is 24
125
|__________|<--0x8049d7c
126
|__________|<--0x8049d94
127
|__________|<--0x8049dac
128
|__________|<--0x8049dc4
129
|__________|<--0x8049ddc
130
|__________|<--0x8049df4
131
|__________|<--0x8049e0c
132
|__________|<--0x8049e24
133
|__________|<--0x8049e3c
134
|__________|<--0x8049e54
135
|__________|<--0x8049e6c
136
|__________|<--0x8049e84
137
|__________|<--0x8049e9c
138
|__________|<--0x8049eb4
139
|__________|<--0x8049ecc
140
|__________|<--0x8049ee4
141
|__________|<--0x8049efc
142
|__________|<--0x8049f14
143
|__________|<--0x8049f2c
144
|__________|<--0x8049f44
145
|__________|<--0x8049f5c
146
|__________|<--0x8049f74
147
|__________|<--0x8049f8c
148
|__________|<--0x8049fa4
149
the address of head(type of head_list) is:0x8049c74
150
the address of member1 is: 0x8049d7c
151
the address of member2 is: 0x8049e3c
152
the address of member3 is: 0x8049e9c
153
the address of member4 is: 0x8049efc
154
Now we'll test the value aear of ts_hlist>>>>>
155
member2 of rs is:3
156
member3 of rs is:A
157
member4 of rs is:44.33

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

分类:
Search Engine
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律