标签:com http blog style class div img code c log t
1。结构的存储分配
1
2 |
printf ( "%d \n" , sizeof ( char )); printf ( "%d \n" , sizeof ( int )); |
int 类型为4B char 为1B
1
2
3
4
5
6
7 |
struct
sa { char
a; int
b; char
c; }; |
1
2
3
4
5
6
7
8 |
struct
sa { char
c; char
b; int
a; }; struct
sa ssa; |
1 |
printf ( "%d \n" ,offsetof( struct
sa,a)); |
结构体存储时要注意
要满足字对齐,起始地址为四的倍数,结束为止为4 的倍数。
1
2
3
4
5
6
7
8
9
10
11
12
13 |
struct
sa { char
a; char
b; double
e; int
d; }; struct
sa ssa; printf ( "%d \n" ,offsetof( struct
sa,a)); printf ( "%d \n" ,offsetof( struct
sa,b)); printf ( "%d \n" ,offsetof( struct
sa,e)); printf ( "%d \n" ,offsetof( struct
sa,d)); |
2.结构体作函数的参数
部分值传递
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
#include <stdio.h> #include <stdlib.h> #include <stddef.h> struct
sa { char
a; char
b; double
e; int
d; }; char
saf( struct
sa ssa) { return
ssa.a+ssa.b; } int
main() { char
ra; struct
sa ssa={1,2,1.1,4}; ra=saf(ssa); printf ( "%d \n" ,ra); } |
引用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
#include <stdio.h> #include <stdlib.h> #include <stddef.h> struct
sa { char
a; char
b; double
e; int
d; }; void
saf( struct
sa *ssa) { ssa->a=ssa->a+ssa->b; } int
main() { struct
sa saa={1,2,1.1,4},*ssa; ssa=&saa; saf(ssa); printf ( "%d \n" ,ssa->a); } |
3位段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
#include <stdio.h> #include <stdlib.h> #include <stddef.h> struct
sa { unsigned a :1; unsigned b :2; signed
e :2; signed
d :2; }; int
main() { struct
sa saa={1,2,3,4},*ssa; printf ( "%d \n" , sizeof ( struct
sa)); printf ( "%d \n" ,saa.d); } |
d占2Bit,给d赋值 4溢出 ,为00,有符号数 结果为0,
给d 赋值 3 溢出,为11,有符号数,结果为-1;
位段的功能均可由移位和屏蔽实现。
标签:com http blog style class div img code c log t
原文地址:http://www.cnblogs.com/chen-/p/3702112.html