[关闭]
@lishuhuakai 2015-05-17T09:26:09.000000Z 字数 1111 阅读 1495

结构体练习

1.自己定义一个数据类型 数据类型的本质是固定大小内存块的别名
2.注意结构体类型定义变量的时候,c和c++编译器的处理行为不一样。
3.结构体类型typedef
4.结构变量内存四字节存放

  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. }

练习 c

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注