1.变量的存放位置

image.pngimage.png注意:volatile
volatile是一个特征修饰符(type specifier).volatile的作用是作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值。

2.结构体的存在空间

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include <stdlib.h>
  4. struct person {
  5. char* name;
  6. int age;
  7. };
  8. struct person2 {
  9. char name[100];
  10. int age;
  11. };
  12. struct person3 {
  13. char sex;
  14. int age;
  15. };
  16. struct person4 {
  17. char sex;
  18. char high;
  19. int age;
  20. };
  21. struct company {
  22. char* name;
  23. struct person worker[100];
  24. };
  25. int main(void)
  26. {
  27. struct person wei = { "weidongshan", 40 };
  28. printf("sizeof(int) = %d, sizeof(char) = %d\r\n", sizeof(int), sizeof(char));
  29. printf("sizeof(int *) = %d, sizeof(char *) = %d\r\n", sizeof(int*), sizeof(char*));
  30. printf("sizeof(struct person) = %d, sizeof(struct person2) = %d\r\n", \
  31. sizeof(struct person), sizeof(struct person2));
  32. printf("sizeof(struct person *) = %d, sizeof(struct person2 *) = %d\r\n", \
  33. sizeof(struct person*), sizeof(struct person2*));
  34. printf("sizeof(struct person3) = %d, sizeof(struct person3 *) = %d\r\n", \
  35. sizeof(struct person3), sizeof(struct person3*));
  36. printf("sizeof(struct person4) = %d, sizeof(struct person4 *) = %d\r\n", \
  37. sizeof(struct person4), sizeof(struct person4*));
  38. printf("name = %s, age = %d\r\n", wei.name, wei.age);
  39. while (1);
  40. return 0;
  41. }

image.png

3.结构体的存在空间

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include <stdlib.h>
  4. typedef struct student {
  5. char* name;
  6. int age;
  7. struct student* classmate;
  8. }student, * pstudent;
  9. int main(void)
  10. {
  11. student zhangsan = { "zhangshan", 10, NULL };
  12. student lili = { "lili", 10, NULL };
  13. zhangsan.classmate = &lili;
  14. lili.classmate = &zhangsan;
  15. printf("zhangsan's classmate is :%s\r\n", zhangsan.classmate->classmate->classmate->name);
  16. while (1);
  17. return 0;
  18. }

image.png

4.函数指针

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include <stdlib.h>
  4. typedef struct student {
  5. char* name;
  6. int age;
  7. void (*good_work)(void);
  8. struct student* classmate;
  9. }student, * pstudent;
  10. static void play_ball(void)
  11. {
  12. printf("playing ball\r\n");
  13. }
  14. static void sing_song(void)
  15. {
  16. printf("singing\r\n");
  17. }
  18. int main(void)
  19. {
  20. int i;
  21. //函数有特权可以不加取址符号
  22. student ss[2] = { {"zhangshan", 10, &play_ball, NULL}, {"lili", 10, sing_song, NULL} };
  23. for (i = 0; i < 2; i++)
  24. {
  25. ss[i].good_work();
  26. }
  27. while (1);
  28. return 0;
  29. }

image.png

5.函数指针的使用

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include <stdlib.h>
  4. typedef struct lcd_operation {
  5. int type;
  6. void (*draw_logo)(void);
  7. }lcd_operation, * p_lcd_operation;
  8. int read_eeprom()
  9. {
  10. /* 0: lcd a
  11. * 1: lcd b
  12. */
  13. return 0;
  14. }
  15. int get_lcd_type(void)
  16. {
  17. return read_eeprom();
  18. }
  19. void draw_logo_lcda(void)
  20. {
  21. printf("display logo on lcd a\r\n");
  22. }
  23. void draw_logo_lcdb(void)
  24. {
  25. printf("display logo on lcd b\r\n");
  26. }
  27. lcd_operation xxx_com_lcds[2] = {
  28. {0, draw_logo_lcda},
  29. {1, draw_logo_lcdb},
  30. };
  31. p_lcd_operation get_lcd(void)
  32. {
  33. int type = get_lcd_type();
  34. return &xxx_com_lcds[type];
  35. }
  36. //结构体成员的初始化
  37. lcd_operation lcd_a_ops = {
  38. .type = 0,
  39. .draw_logo = draw_logo_lcda,
  40. };
  41. //add_type是个函数指针,返回值为int,参数为(int,int)
  42. typedef int (*add_type)(int, int);
  43. add_type f1;
  44. add_type f2;
  45. int (*f3)(int, int);
  46. int (*f4)(int, int);
  47. int main(void)
  48. {
  49. p_lcd_operation lcd;
  50. lcd = get_lcd();
  51. lcd->draw_logo();
  52. while (1);
  53. return 0;
  54. }

image.png

6.单向链表的使用

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include <stdlib.h>
  4. typedef struct spy {
  5. char* name;
  6. struct spy* next;
  7. }spy, * p_spy;
  8. spy A = { "A", NULL };
  9. spy B = { "B", NULL };
  10. spy C = { "C", NULL };
  11. spy D = { "D", NULL };
  12. spy E = { "E", NULL };
  13. p_spy head = NULL;
  14. //链表的尾插法
  15. void insert_spy(p_spy newspy)
  16. {
  17. p_spy last;
  18. if (head == NULL)
  19. {
  20. head = newspy;
  21. newspy->next = NULL;
  22. }
  23. else
  24. {
  25. /* 先找到链表的最后一项 last */
  26. last = head;
  27. while (last)
  28. {
  29. if (last->next == NULL) /* 找到了 */
  30. break;
  31. else
  32. last = last->next;
  33. }
  34. /* */
  35. last->next = newspy;
  36. newspy->next = NULL;
  37. }
  38. }
  39. //链表的删除
  40. void remove_spy(p_spy oldspy)
  41. {
  42. p_spy left;
  43. if (head == oldspy)
  44. {
  45. head = oldspy->next;
  46. }
  47. else
  48. {
  49. /* 找出oldspy的上线 */
  50. left = head;
  51. while (left)
  52. {
  53. if (left->next == oldspy)
  54. break;
  55. else
  56. left = left->next;
  57. }
  58. if (left)
  59. {
  60. left->next = oldspy->next;
  61. }
  62. }
  63. }
  64. void print_spy(void)
  65. {
  66. p_spy tmp = head;
  67. while (tmp)
  68. {
  69. printf("%s\r\n", tmp->name);
  70. tmp = tmp->next;
  71. }
  72. }
  73. int main(void)
  74. {
  75. /*
  76. A.next = &B;
  77. B.next = &C;
  78. C.next = NULL;
  79. */
  80. insert_spy(&A);
  81. insert_spy(&B);
  82. insert_spy(&C);
  83. insert_spy(&D);
  84. head = &A;
  85. print_spy();
  86. remove_spy(&D);
  87. printf("remove spy B: \r\n");
  88. print_spy();
  89. system("pause");
  90. return 0;
  91. }

image.png