结构体

数组允许定义可存储相同类型数据项的变量,而结构体是C编程中另一种用户自定义的可用的数据类型,它允许用户可以存储不同类型的数据项。

struct 语句的格式如下:

  1. struct [structure tag]
  2. {
  3. member definition;
  4. member definition;
  5. ...
  6. member definition;
  7. } [one or more structure variables];

其中,structure tag 是可选的。在结构定义的末尾,最后一个分号之前,还可以指定一个或多个结构变量,这是可选的。

下面是一个定义结构和访问结构成员的例子:

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct Books {
  5. int id;
  6. char title[];
  7. char author[];
  8. char subject[];
  9. };
  10.  
  11. int main() {
  12. // 声明book1,类型为 Books
  13. struct Books book1;
  14. // 添加信息到book1
  15. book1.id = ;
  16. strcpy(book1.title, "Learn C program");
  17. strcpy(book1.author, "Zhang San");
  18. strcpy(book1.subject, "IT/Program");
  19. // 输出book1信息
  20. printf("%d\n", book1.id); //
  21. printf("%s\n", book1.title); // Learn C program
  22. printf("%s\n", book1.author); // Zhang San
  23. printf("%s\n", book1.subject); // IT/Program
  24. }

使用typedef关键字可以为类型取一个新的名字。例如,可以对结构体使用typedef来定义一个新的数据类型名字,然后使用这个新的数据类型来直接定义结构变量。

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef struct Books {
  5. int id;
  6. char title[];
  7. char author[];
  8. char subject[];
  9. } Book;
  10.  
  11. int main() {
  12. // 声明book1,类型为 Books
  13. Book book1;
  14. // 添加信息到book1
  15. book1.id = ;
  16. strcpy(book1.title, "Learn C program");
  17. strcpy(book1.author, "Zhang San");
  18. strcpy(book1.subject, "IT/Program");
  19. // 输出book1信息
  20. printf("%d\n", book1.id); //
  21. printf("%s\n", book1.title); // Learn C program
  22. printf("%s\n", book1.author); // Zhang San
  23. printf("%s\n", book1.subject); // IT/Program
  24. return ;
  25. }

结构也可以作为函数参数,并使用指针。指针变量访问结构的成员,必须使用 -> 运算符。

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct Books {
  5. int id;
  6. char title[];
  7. char author[];
  8. char subject[];
  9. };
  10.  
  11. void printBook(struct Books *book);
  12.  
  13. int main() {
  14. // 声明book1,类型为 Books
  15. struct Books book1;
  16. // 添加信息到book1
  17. book1.id = ;
  18. strcpy(book1.title, "Learn C program");
  19. strcpy(book1.author, "Zhang San");
  20. strcpy(book1.subject, "IT/Program");
  21. printBook(&book1);
  22. }
  23.  
  24. void printBook(struct Books *book) {
  25. printf("%s\n", book->title);
  26. printf("%s\n", book->author);
  27. printf("%s\n", book->subject);
  28. printf("%d\n", book->id);
  29. }

共用体

共用体是一种特殊的数据类型,允许用户在相同的内存位置存储不同的数据类型。可以定义一个带有多成员的共用体,但是任何时候只能有一个成员带有值。共用体提供了一种使用相同的内存位置的有效方式。

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. // 从下面例子可以看出
  5. // (1) 共用体占用的内存为共用体中最大的成员所占的内存。
  6. // (2) 最后赋的值才是有效值。
  7. union Data {
  8. int i; // 4 Byte
  9. float f; // 4 Byte
  10. char c[]; // 20 Byte
  11. };
  12.  
  13. int main() {
  14. union Data data;
  15. printf("Memory: %d\n", sizeof(data)); //
  16. data.i = ;
  17. data.f = 20.5;
  18. strcpy(data.c, "This is a C program.");
  19. printf("data.i: %d\n", data.i); // 损坏值
  20. printf("data.f: %f\n", data.f); // 损坏值
  21. printf("data.c: %s\n", data.c); // This is a C program.
  22. return ;
  23. }

位域

把一个(或多个)字节中的二进位划分为几个不同的区域,并说明每个区域的位数。每个域有一个域名,允许在程序中按域名进行操作。这样就可以把几个不同的对象用一个(或多个)字节的二进制位域来表示。

注意事项:

(1) 一个位域必须存储在同一个字节中,不能跨两个字节。(说明一个位域不能超过8位)

(2) 一个字节所剩空间不够存放另一位域时,应从下一单元起存放该位域。(也可以用空域占位,使某位域从下一单元开始。)

  1. #include <stdio.h>
  2.  
  3. struct bs {
  4. unsigned a :;
  5. unsigned b :;
  6. unsigned :; // 空域
  7. unsigned c :;
  8. } bit, *pbit;
  9. int main() {
  10. bit.a = ;
  11. bit.b = ;
  12. bit.c = ;
  13. printf("%d\t%d\t%d\n", bit.a, bit.b, bit.c); // 1 15 255
  14. pbit = &bit; // *pbit要初始化!
  15. pbit->a = ;
  16. pbit->b &= ; // 与运算
  17. pbit->c |= ; // 或运算
  18. printf("%d\t%d\t%d\n", pbit->a, pbit->b, pbit->c); // 0 7 255
  19. }

C基础知识(8):结构体、共用体、位域的更多相关文章

  1. C++结构、共用体、枚举

    一.结构 结构是C++OOP的基石.学习有关结构的知识僵尸我们离C++的核心OOP更近. 结构是用户定义的类型,同一个结构可以存储多种类型数据,这使得将一个事物的不同属性构成一个对象成为了可能.另外C ...

  2. 5、数组&字符串&结构体&共用体&枚举

    程序中内存从哪里来 三种内存来源:栈(stack).堆(heap).数据区(.date): 栈(stack) 运行自动分配.自动回收,不需要程序员手工干预: 栈内存可以反复使用: 栈反复使用后,程序不 ...

  3. C语言高级-结构,共用体,文件,链表

    C语言结构 标准声明方式 struct student{        int age;        char sex;    }; 这个可以在main函数中定义:  struct student ...

  4. C语言基础 (11) 结构体 ,共用体 枚举 typedef

    1 课堂回顾 作用域与生命周期 2 static 局部变量 2 打字游戏 3 内存分区代码分析 4 结构体基本操作 (复合类型[自定义类型 #include <stdio.h> #incl ...

  5. 瘋子C语言笔记(结构体/共用体/枚举篇)

    (一)结构体类型 1.简介: 例: struct date { int month; int day; int year; }; struct student { int num; char name ...

  6. C++复合类型(结构,共用体,枚举)

    •结构是用户定义的类型,而结构的声明定义了这种类型的数据属性. 一.关键字struct声明:   定义了一种新类型 struct inflatable{ char name[20];//结构成员 fl ...

  7. CSS基础知识—【结构、层叠、视觉格式化】

    结构和层叠 选择器的优先级顺序: style[内联元素]选择器>Id选择器>类选择器 属性选择器>元素选择器>通配器选择器 重要性:@important 有这个标记的属性值,优 ...

  8. iOS底层基础知识-文件目录结构

    一:iOS沙盒知识 出于安全考虑,iOS系统把每个应用以及数据都放到一个沙盒(sandbox)里面,应用只能访问自己沙盒目录里面的文件.网络资源等(也有例外,比如系统通讯录.照相机.照片等能在用户授权 ...

  9. C语言基础知识-循环结构

    用while打印出1~100之间7的倍数    int i = 1;     while循环是当条件表达式的结果为真时,执行大括号里面的循环体,重复执行直到条件表达式的结果为假时结束循环.     w ...

  10. java基础知识—循环结构

    1.while 循环 语法: while(循环操作){ 循环操作: } 特点:先判断,再执行:2. == : 用于数字比较 比较的是地址 equals: 用于字符串比较 比较的是字符 3.do-whi ...

随机推荐

  1. Android Multiple dex files define 解决包冲突

    这段时间有一个新需求,安卓App通过URL在线预览PDF.选择使用并倒入PdfViewPager库时,报了如下异常: jdmerchants:transformDexArchiveWithExtern ...

  2. 原来你是这样的 jsonp(原理与具体实现细节)

    前言 原文地址 仓库地址 jsonp(JSON with padding)你一定不会陌生,前端向后端拿数据的方式之一,也是处理跨域请求的得利助手. 我们早已习惯,早已熟练了jQ或者zepto的ajax ...

  3. shell通配符

    wildcard 通配服   匹配.c文件 *.sh----常看当前目录下sh文件 *.c----常看当前目录下c文件 []---表示中括号 e.g [0,1,2,3,4]----能匹配0,1,2,3 ...

  4. 2019CCPC秦皇岛赛区(重现赛)- F

    链接: http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1006&cid=872 题意: Z 国近年来一直在考虑遏制国土沙 ...

  5. 【GDKOI2018】总结

    前言 车祸现场... day1 T1:其实就是对于每个点的有用的时间点建一个点,然后连边,对于询问(x,y),从点(y,inf),往回走,能走到的最早的x的时间点就是答案. 比赛上用最后的一个多小时来 ...

  6. AtCoder AGC005E Sugigma: The Showdown (博弈论)

    题目链接 https://atcoder.jp/contests/agc005/tasks/agc005_e 题解 完了真的啥都不会了-- 首先,显然如果某条A树的边对应B树上的距离大于等于\(3\) ...

  7. 28.Python list列表详解

    在实际开发中,经常需要将一些(不只一个)数据暂储起来,以便将来使用.说到这里,一些读者可能知道或听说过数组,它就可以把多个数据挨个存储到一起,通过数组下标可以访问数组中的各个元素.但使用数组存储数据有 ...

  8. Vue_(组件通讯)非父子关系组件通信

    Vue单项数据流 传送门 Vue中不同的组件,即使不存在父子关系也可以相互通信,我们称为非父子关系通信 我们需要借助一个空Vue实例,在不同的组件中,使用相同的Vue实例来发送/监听事件,达到数据通信 ...

  9. 访问zabbix首页无法正常登陆

    访问: http://IP/zabbix/ (1) You should see the first screen of the frontend installation wizard. (2) 检 ...

  10. $\LaTeX$数学公式大全6

    $6\ Binary\ Operation/Relation\ Symbols$$\ast$ \ast$\star$ \star$\cdot$ \cdot$\circ$ \circ$\bullet$ ...