[关闭]
@yanglt7 2018-08-06T00:09:57.000000Z 字数 4616 阅读 797

第10周:枚举、结构、联合

C


10.1.1 枚举:枚举

常量符号化

枚举

套路:自动计数的枚举

枚举量

枚举只是int

10.2.1 结构:结构类型

声明结构类型

  1. #include <stdio.h>
  2. int main(int argc, char const *argv[])
  3. {
  4. struct date{
  5. int month;
  6. int day;
  7. int year;
  8. };
  9. struct date today;
  10. today.month = 04;
  11. today.day = 16;
  12. today.year = 2018;
  13. printf("Today's date is %i-%i-%i.\n",
  14. today.year,today.month,today.day);
  15. return 0;
  16. }
  17. Today's date is 2018-4-16.

在函数内/外?

声明结构的形式

  1. struct point{
  2. int x;
  3. int y;
  4. };
  5. struct point p1,p2;
  6. p1p2都是point,里面有xy的值
  7. struct{
  8. int x;
  9. int y;
  10. } p1,p2;
  11. p1p2都是一种无名结构,里面有xy
  12. struct point{
  13. int x;
  14. int y;
  15. } p1,p2;
  16. p1p2都是point,里面有xy的值

结构变量

结构初始化

结构成员

结构运算

结构指针

  1. #include <stdio.h>
  2. int main(int argc, char const *argv[])
  3. {
  4. struct date{
  5. int month;
  6. int day;
  7. int year;
  8. };
  9. struct date today;
  10. today=(struct date){04,16,2018};
  11. struct date *pDate = &today;
  12. printf("Today's date is %i-%i-%i.\n",
  13. today.year,today.month,today.day);
  14. printf("address of today is %p\n", pDate);
  15. return 0;
  16. }

10.2.2 结构:结构与函数

结构作为函数参数

输入结构

  1. #include <stdio.h>
  2. struct point {
  3. int x;
  4. int y;
  5. };
  6. void getStruct(struct point);
  7. void output(struct point);
  8. int main(int argc, char const *argv[])
  9. {
  10. struct point y = {0,0};
  11. getStruct(y);
  12. output(y);
  13. }
  14. void getStruct(struct point p)
  15. {
  16. scanf("%d", &p.x);
  17. scanf("%d", &p.y);
  18. printf("%d, %d\n", p.x, p.y);
  19. }
  20. void output(struct point p)
  21. {
  22. printf("%d, %d", p.x, p.y);
  23. }
  24. 12 23
  25. 12,23
  26. 0,0

解决的方案

  1. #include <stdio.h>
  2. struct point {
  3. int x;
  4. int y;
  5. };
  6. struct point getStruct(void);
  7. void output(struct point);
  8. int main(int argc, char const *argv[])
  9. {
  10. struct point y = {0,0};
  11. y = getStruct();
  12. output(y);
  13. }
  14. struct point getStruct(void)
  15. {
  16. struct point p;
  17. scanf("%d", &p.x);
  18. scanf("%d", &p.y);
  19. printf("%d, %d\n", p.x, p.y);
  20. return p;
  21. }
  22. void output(struct point p)
  23. {
  24. printf("%d, %d", p.x, p.y);
  25. }
  26. 12 23
  27. 12, 23
  28. 12, 23

结构指针作为参数

指向结构的指针

  1. struct date {
  2. int month;
  3. int day;
  4. int year;
  5. } myday;
  6. struct date *p = &myday;
  7. (*p).month = 12;
  8. p -> month = 12;

10.2.3 结构:结构中的结构

结构数组

结构中的结构

  1. struct dateAndTime {
  2. struct date sdate;
  3. struct time stime;
  4. };

嵌套的结构

  1. struct point {
  2. int x;
  3. int y;
  4. };
  5. struct rectangle {
  6. struct point pt1;
  7. struct point pt2;
  8. };
  9. 如果有变量
  10. struct rectangle r;
  11. 就可以有:
  12. r.pt1.x r.pt1.y
  13. r.pt2.x r.pt2.y
  14. 如果有变量定义:
  15. struct rectangle r, *rp;
  16. rp = &r;
  17. 那么下面的四种形式是等价的:
  18. r.pt1.x
  19. rp->pt1.x
  20. (r.pt1).x
  21. (rp->pt1).x
  22. 但是没有rp->pt1->x (因为pt1不是指针)

结构中的结构的数组

  1. #include <stdio.h>
  2. struct point{
  3. int x;
  4. int y;
  5. };
  6. struct rectangle {
  7. struct point p1;
  8. struct point p2;
  9. };
  10. void printRect(struct rectangle r)
  11. {
  12. printf("<%d, %d> to <%d, %d>\n", r.p1.x, r.p1.y, r.p2.x, r.p2.y);
  13. }
  14. int main(int argc, char const *argv[])
  15. {
  16. int i;
  17. struct rectangle rects[] = {
  18. {{1,2}, {3,4}},
  19. {{5,6}, {7,8}}
  20. };// 2 rectangles
  21. for(i=0;i<2;i++) {
  22. printRect(rects[i]);
  23. }
  24. }
  25. <1, 2> to <3, 4>
  26. <5, 6> to <7, 8>

10.3.1 联合:类型定义

自定义数据类型(typedef)

Typedef

  1. typedef long int64_t; //重载已有的类型名字,新名字的含义更清晰,具有可移植性
  2. typedef struct ADate {
  3. int month;
  4. int day;
  5. int year;
  6. } Date; //简化了复杂的数字
  7. int 64_t i = 1000000000;
  8. Date d = {4,17,2018};
  1. typedef int Length;//Length 就等价于int类型
  2. typedef *char[10] Strings;//Strings 是10个字符串的数组的类型
  3. typedef struct node {
  4. int data;
  5. struct node *next;
  6. } aNode;
  7. typedef struct node aNode;//这样用aNode就可以代替struct node

10.3.2 联合:联合

联合

  1. union AnElt {
  2. int i;
  3. char c;
  4. } elt1,elt2;
  5. elt1.i = 4;
  6. elt2.c = 'a';
  7. elt2.i = 0xDEADBEEF;

union的用处

  1. #include <stdio.h>
  2. typedef union {
  3. int i;
  4. char ch[sizeof(int)];
  5. } CHI;
  6. int main(int argc, char const *argv[])
  7. {
  8. CHI chi;
  9. int i;
  10. chi.i = 1234;
  11. for (i=0; i<sizeof(int); i++) {
  12. printf("%02hhX", chi.ch[i]);
  13. }
  14. printf("\n");
  15. return 0;
  16. }
  17. FFD2040000
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注