c 函数及指针学习 7
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;
位段的功能均可由移位和屏蔽实现。
c 函数及指针学习 7的更多相关文章
- C函数及指针学习1
1 大段程序注释的方法 #if 0#endif 2三字母词 以两个问号 开始的都要注意 3 字面值(常量) 在整型号字面值后加 字符L (long),U(unsigned)说明字符常量 为长整型 或( ...
- c 函数及指针学习 10
标准库函数 1算数运算stdlib.h 2随机数stdlib.h 3字符串转化stdlib.h 4数学函数 math.h 5日期和时间 time.h 6信号 signal.h 7打印可变参数列表std ...
- c 函数及指针学习 9
指针的高级应用 处理命令行参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <stdio.h> int main(int ar ...
- c 函数及指针学习 5
聚合数据类型 能够同时存储超过一个的单独数据. c语言提供了数组和结构体. 1. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> # ...
- c 函数及指针学习 4
1数组和指针声明的差别 声明数组:为数组分配内存,为数组名分配内存(指针常量 4个字节) 指针:为指针分配内存(指针变量 4个字节) 1 2 3 4 5 6 7 8 9 10 #include < ...
- c 函数及指针学习 3
strlen(x) 返回 size_t 类型,size_t是 unsigned int 类型,所以 strlen(x)-strlen(y) 返回 unsigned int 始终 >=0 1 2 ...
- C函数及指针学习2
1.break 永久终止循环,continue 结束当前循环 2.switch 每个case标签要有唯一值,(且为常量或常量表达式) 不加break 时执行流会贯穿整个case 标签 3 赋值操作符 ...
- c 函数及指针学习 8
联合体 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> union sa { double a; int b; ...
- c 函数及指针学习 6
不完整声明 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 26 /* 方法一 */ struct tag_a{ ...
随机推荐
- CoHTMLDocument
http://blog.csdn.net/dlwxn/article/details/2860329 http://www.itnose.net/detail/120267.html 不知道是 线程内 ...
- java.lang.InstantiationException
java.lang.InstantiationException 出现这种异常的原因通常情况下是由于要实例化的对象是一个接口或者是抽象类等无法被实例化的类.
- 使用Google API 下拉刷新或者增加数据 SwipeRefreshLayout
贴出布局代码: <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/id_swipe_ly" and ...
- 支持单色条码图像生成的条形码控件Barcode Professional
Barcode Professional for .NET Windows Forms条形码控件是一款灵活和强大的.NET组件(.NET DLL 类库),它让您轻松地添加条码生成和打印功能到您的.NE ...
- STM32之RTC配置与初始化-rtc.h rtc.c
<rtc.h> #include "stm32f10x.h" #ifndef _RTC_H #define _RTC_H typedef struct { vu8 ho ...
- Git搭建团队开发环境操作演练
模拟创建远程git仓库 1.首先创建如下目录结构: /Users/hujh/Desktop/GitTest2/GitServer/weibo weibo是我们要创建的项目 2.切换目录 $ cd /U ...
- 数组的foreach方法和jQuery中的each方法
/* * 数组的forEach方法: * 1.返回给回调的参数先是值,然后是下标 * 2.回调函数执行时内部的this指向window * */ /*var arr = [1,2,3,4,5]; ar ...
- PAT 05-树8 Huffman Codes
以现在的生产力,是做不到一天一篇博客了.这题给我难得不行了,花了两天时间在PAT上还有测试点1没过,先写上吧.记录几个做题中的难点:1.本来比较WPL那块我是想用一个函数实现的,无奈我对传字符串数组无 ...
- 玩转无线电 -- 温哥华天车 RFID 票务系统
0x00 前言 如今物联网 RFID系统已经完全融入了我们的生活当中. 从楼宇门禁到 Apple Pay. 可以说其身影是无处不在.很多网友也分享了自己对RFID系统的安全测试心得.不过大多还是基于门 ...
- kali 2016的基础配置
1.Kali 2016的更新源 deb http://http.kali.org/kali kali-rolling main contrib non-free 2.安装虚拟机 apt-get upd ...