[关闭]
@lishuhuakai 2015-05-17T17:18:11.000000Z 字数 3268 阅读 1526

结构体进阶

c

结构体的基础操作

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //1.自己定义一个数据类型,数据类型的本质是固定大小内存块的别名
  5. //2..c和.cpp定义结构体类型定义变量的时候,c和c++编译器的处理行为不一样
  6. //3.结构体类型typedef
  7. //4.结构变量内存四字节存放,
  8. typedef struct Teacher
  9. {
  10. char name[62];
  11. int age;
  12. char title[128];
  13. /************************************************************************/
  14. /*
  15. char name[62]; // 62字节
  16. char c1; //1字节
  17. char c2; //1字节
  18. int age; //4字节
  19. 一共68字节 */
  20. /************************************************************************/
  21. /************************************************************************/
  22. /*
  23. char name[62]; //62字节
  24. char c1; //63 + 1空闲字节
  25. int age; //4字节
  26. char c2; //1字节 + 3空闲字节
  27. 一共72字节 */
  28. /************************************************************************/
  29. }Teacher;
  30. //结构体变量的初始化3种方法
  31. //结构体定义变量的4种方法
  32. //结构体变量的复制方法,操作结构体变量
  33. void main()
  34. {
  35. //告诉编译器要分配内存
  36. struct Teacher t1; /*使用struct Teacher直接定义*/
  37. Teacher t2 = { "dddd", 40, "dffd" };/*只有使用了tyededef重命名了结构体才能这样用*/
  38. /*常用的初始化方法就上面一种*/
  39. printf("sizeof(char):%d\n", sizeof(char));
  40. t1.age = 10; /*通过.直接赋值*/
  41. Teacher *p = NULL; /*通过指针间接赋值*/
  42. p = &t1;
  43. p->age = 20;
  44. printf("sizeof(Teacher):%d\n", sizeof(t1));
  45. system("pause");
  46. }

结构体进阶

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct Teacher
  5. {
  6. char name[62];
  7. int age;
  8. }Teacher;
  9. void printfArray(Teacher *p, int count)
  10. {
  11. for (int i = 0; i < count; ++i)
  12. {
  13. printf("%d\n", p[i].age);
  14. }
  15. }
  16. Teacher *createTarray(int count)
  17. {
  18. Teacher *p = (struct Teacher *)malloc(count * sizeof(Teacher));
  19. if (p == NULL)
  20. {
  21. return;
  22. }
  23. for (int i = 0; i < count; ++i)
  24. {
  25. memset(&p[i], 0, sizeof(Teacher));
  26. //或者 memset(p1 + i, 0, sizeof(Teacher));
  27. }
  28. return p;
  29. }
  30. int sortArray(Teacher *p, int num)
  31. {
  32. Teacher tmp;
  33. for (int i = 0; i < num; ++i)
  34. {
  35. for (int j = i + 1; j < num; ++j)
  36. {
  37. if (p[i].age < p[j].age)
  38. {
  39. memcpy(&tmp, &p[i], sizeof(Teacher));
  40. memcpy(&p[i], &p[j], sizeof(Teacher));
  41. memcpy(&p[j], &tmp, sizeof(Teacher));
  42. }
  43. }
  44. }
  45. return 0;
  46. }
  47. void freeMem(Teacher *p, int count)
  48. {
  49. if (p != NULL)
  50. {
  51. free(p);
  52. p = NULL; //垃圾,写了和没写一个样
  53. }
  54. // for (int i = 0; i < count; ++i)
  55. // {
  56. // if (p[i] != NULL)
  57. // {
  58. // free(p[i]);
  59. // }
  60. // }
  61. }
  62. void main()
  63. {
  64. Teacher tArray[10];
  65. Teacher *pArray = createTarray(4); //在堆上分配内存
  66. for (int i = 0; i < 4; ++i)
  67. {
  68. printf("请输入age:");
  69. scanf_s("%d", &tArray[i].age); //要改变实参的值
  70. }
  71. printfArray(tArray, 4);
  72. sortArray(tArray, 4);
  73. printfArray(tArray, 4);
  74. system("pause");
  75. }

包含指针的结构体

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct Teacher
  5. {
  6. char name[62];
  7. char *a_name;
  8. int age;
  9. }Teacher;
  10. void printfArray(Teacher *p, int count)
  11. {
  12. for (int i = 0; i < count; ++i)
  13. {
  14. printf("%s %d\n", p[i].a_name, p[i].age);
  15. }
  16. }
  17. Teacher *createTarray(int count)
  18. {
  19. Teacher *p = (struct Teacher *)malloc(count * sizeof(Teacher));
  20. if (p == NULL)
  21. {
  22. return NULL;
  23. }
  24. for (int i = 0; i < count; ++i)
  25. {
  26. memset(&p[i], 0, sizeof(Teacher));
  27. p[i].a_name = (char *)malloc(128 * sizeof(char));
  28. memset(p[i].a_name, 0, 128 * sizeof(char));
  29. //或者 memset(p1 + i, 0, sizeof(Teacher));
  30. }
  31. return p;
  32. }
  33. int sortArray(Teacher *p, int num)
  34. { //按照年龄排序
  35. Teacher tmp;
  36. for (int i = 0; i < num; ++i)
  37. {
  38. for (int j = i + 1; j < num; ++j)
  39. {
  40. if (p[i].age < p[j].age)
  41. {
  42. memcpy(&tmp, &p[i], sizeof(Teacher));
  43. memcpy(&p[i], &p[j], sizeof(Teacher));
  44. memcpy(&p[j], &tmp, sizeof(Teacher));
  45. }
  46. }
  47. }
  48. return 0;
  49. }
  50. void freeMem(Teacher *p, int count)
  51. {
  52. if (p == NULL)
  53. {
  54. return;
  55. }
  56. for (int i = 0; i < count; ++i)
  57. {
  58. if (p[i].a_name != NULL)
  59. {
  60. free(p[i].a_name);
  61. }
  62. }
  63. if (p != NULL) //这里特别小心,怎样分配,怎样释放
  64. {
  65. free(p);
  66. p = NULL; //垃圾代码,写了和没写一个样
  67. }
  68. }
  69. void main()
  70. {
  71. Teacher tArray[10];
  72. Teacher *pArray = createTarray(4); //在堆上分配内存
  73. for (int i = 0; i < 4; ++i)
  74. {
  75. printf("请输入别名:");
  76. scanf_s("%s", pArray[i].a_name); //要改变实参的值
  77. printf("请输入age:");
  78. scanf_s("%d", &tArray[i].age); //要改变实参的值
  79. }
  80. printfArray(tArray, 4);
  81. sortArray(tArray, 4);
  82. printfArray(tArray, 4);
  83. freeMem(pArray, 4); //释放堆上的数据
  84. system("pause");
  85. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注