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的更多相关文章

  1. C函数及指针学习1

    1 大段程序注释的方法 #if 0#endif 2三字母词 以两个问号 开始的都要注意 3 字面值(常量) 在整型号字面值后加 字符L (long),U(unsigned)说明字符常量 为长整型 或( ...

  2. c 函数及指针学习 10

    标准库函数 1算数运算stdlib.h 2随机数stdlib.h 3字符串转化stdlib.h 4数学函数 math.h 5日期和时间 time.h 6信号 signal.h 7打印可变参数列表std ...

  3. 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 ...

  4. c 函数及指针学习 5

    聚合数据类型 能够同时存储超过一个的单独数据. c语言提供了数组和结构体. 1. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> # ...

  5. c 函数及指针学习 4

    1数组和指针声明的差别 声明数组:为数组分配内存,为数组名分配内存(指针常量 4个字节) 指针:为指针分配内存(指针变量 4个字节) 1 2 3 4 5 6 7 8 9 10 #include < ...

  6. c 函数及指针学习 3

    strlen(x) 返回 size_t 类型,size_t是 unsigned int 类型,所以 strlen(x)-strlen(y) 返回 unsigned int 始终 >=0 1 2 ...

  7. C函数及指针学习2

    1.break  永久终止循环,continue 结束当前循环 2.switch 每个case标签要有唯一值,(且为常量或常量表达式) 不加break 时执行流会贯穿整个case 标签 3 赋值操作符 ...

  8. c 函数及指针学习 8

    联合体 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h>   union sa     {     double a;     int b; ...

  9. 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{ ...

随机推荐

  1. C++本质:类的赋值运算符=的重载,以及深拷贝和浅拷贝

    关键词:构造函数,浅拷贝,深拷贝,堆栈(stack),堆heap,赋值运算符摘要:    在面向对象程序设计中,对象间的相互拷贝和赋值是经常进行的操作.    如果对象在申明的同时马上进行的初始化操作 ...

  2. Unity游戏数据用Json保存

    (一)关于路径 unity有几个关键的路径 (1).Application.dataPath 只读路径,就是工作目录的Assets路径 (2).Application.streamingAssetsP ...

  3. iOS多线程之NSThread使用

    iOS中的多线程技术 我们在iOS开发项目过程中,为了解决UI界面操作不被耗时操作阻塞,我们会使用到多线程技术.在iOS开发中,我们主要会用到三种多线程操作技术:NSThread,NSOperatio ...

  4. javascript预解析和作用域

    JavaScript解析过程分为两个阶段: 一是:编译阶段.就是JavaScrip预解析阶段,在这个阶段JavaScript解析器将完成把JavaScript脚本代码转换到字节码; 二是:执行阶段.在 ...

  5. 《JAVA学习笔记 (final关键字)》

    [14-9]面向对象-final关键字 /* 继承的弊端,打破封装性. 不让其他类继承该类,就不会有重写. 怎么能实现呢?通过Java中的一个关键子来实现,final(最终化). [final关键字] ...

  6. MVP架构。。。。

    Model-View-Presenter(MVP)概述    MVC模式已经出现了几十年了,在GUI领域已经得到了广泛的应用,由于微软ASP.NET MVC Framework的出现,致使MVC一度成 ...

  7. hdu 2092

    Ps:wa了两次....一次是从加法那边暴力,然而算法错误..应该从乘法那边暴力破解...然而又没算负数..加上负数..直接暴力AC. 代码: #include "stdio.h" ...

  8. Tomcat容器运行struts2+spring+mybatis架构的java web应用程序简单分析

    1.具体的环境为 MyEclipse 8.5以及自带的tomcat spring3.0.5 struts2.3.15.1 mybatis3.0.5 2.想弄明白的一些问题 tomcat集成spring ...

  9. Code is not literature

    http://www.gigamonkeys.com/code-reading/ I have started code reading groups at the last two companie ...

  10. Python 温习

    关于Python内置函数的示例 Type "copyright", "credits" or "license()" f重写or more ...