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. Echarts 地图控件tooltip多行显示

    直接上代码 var o = { "tooltip": { trigger: 'item', "formatter": function (params) { v ...

  2. java抽象类与接口 详解

    在面向对象的概念中,我们知道所有的对象都是通过类来描绘的,但是并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类往往用来表征我们在对问题 ...

  3. Oracle创建用户并给用户授权查询指定表或视图的权限

    MSV31账户登录数据库进行如下操作: CREATE USER NORTHBOUND IDENTIFIED BY NORTHBOUND  DEFAULT TABLESPACE "TBS_DN ...

  4. 创建和运行shell脚本程序

    转载请标明http://www.cnblogs.com/winifred-tang94/ 要创建一个shell脚本程序,首先新建一个文本文件,然后在这个文本文件中按照shell编程规则输入shell命 ...

  5. 带弹出列表的EditText

    最近做的一个Andriod里有一个这个要求,一个东西需要输入,但同时可以在列表直接选择.看到这个需求,瞬间想到了QQ的登录界面,那个账号输入的控件正式我所需要的. 这个账号输入框右边有一个按钮,点击可 ...

  6. hibernate缓存和提高效率

    1.使用二级缓存,多把大批量的.短期多次的查询数据存到二级缓存中,避免和数据库的多次交互,增加负担.二级缓存加在那些增删改少的,查询多的类中.二级缓存的是对象,如果查出来的不是对象,不会放到缓存中去. ...

  7. xml文件有误

    Unable to start activity ComponentInfo{com.anzi.jmsht.scripturelibrary/com.anzi.jmsht.scripturelibra ...

  8. loaderexceptions

    前段时间遇到一个问题 从容器中取数据时老报一个“无法加载一个或多个请求,请检索loaderexceptions” 真心是不晓得什么问题 以前经常这么用没有问题的 这个是在网站下引用了别的已经编译好的别 ...

  9. XML元素和结点的区别

    1.区别介绍 Element是Node的扩展,所以也更实用一些.例如,用Element可以方便的获得Node的属性getAttribute(String attrName)如果用Node,可以得到一个 ...

  10. hdu 2037

    PS:   - -原本想的是排序开始时间和消耗时间..后来想到可以排序结束时间..后来还wa了一次,因为排序的时候溢出了 思路: 1 3 //13 4 //20 7 3 8 2 9 5 10 //36 ...