定义结构体

从某种程度上来说,会不会用 struct,怎样用 struct 是区别一个开发人员是否具备

丰富开发经历的标志 !

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /*
  4. 怎么区别自定义的struct,所以要加类名
  5. 定义了一个数据类型 固定大小内存块的别名 还没有分配内存
  6. */
  7. // 自定义类 有名结构体 Desk
  8. struct Desk // 类名
  9. {
  10. float length;
  11. float width;
  12. float high;
  13. char mer;
  14. float price;
  15. };
  16. // 自定义类 有名结构体 Person
  17. struct Person
  18. {
  19. float high;
  20. int weight;
  21. char sex;
  22. int age;
  23. float score[4];
  24. };
  25. // 无名结构体City
  26. struct
  27. {
  28. char *name;
  29. char *areaCode;
  30. } city; //定义了变量city
  31. /**
  32. 1 有名结构体一次定义 多处使用,会带来新的命名
  33. 2 无名结构体 定义类型的同时定义变量, 无名结构体不引入新的类型
  34. 这两种方式 用的是偏少一些 多的还是别名结构体
  35. */
  36. // 别名结构体
  37. typedef struct Student
  38. {
  39. char name[100];
  40. char sex;
  41. int age;
  42. }STU;
  43. int main() {
  44. // 自定义的类型
  45. struct Desk d; // 定义了变量 d
  46. struct Person p; //定义了变量 p
  47. printf("%p", &p);
  48. d.length = 10; //成员访问
  49. city.name = "ShangHai";
  50. printf("city.name = %s\n", city.name); //city.name = ShangHai
  51. STU s1;
  52. s1.age = 10;
  53. printf("s1.age=%d\n", s1.age);
  54. // 凡是基本类型 既可定义时初始化 也可以先定义 再赋值
  55. // 凡是构造类型 要么在定义时初始化, 不可以,先定义 再以初始化的方式初始化
  56. STU s2 = { "tom", 'x', 18 }; //初始化 注意 构造类型只能在定义时就要初始化
  57. printf("s2.name=%s\n", s2.name); // s2.name=tom
  58. STU s3;
  59. strcpy(s3.name, "hello");
  60. // s3.name = "Hello"; name是数组的首地址 数组赋值需要下标才可以
  61. printf("s3.name=%s\n", s3.name); //s3.name=hello
  62. //以上都是成员式 下面演示指向式
  63. STU *ps = &s3; //指针初始化
  64. strcpy(ps->name, "World");
  65. ps->age = 20;
  66. printf("ps.name=%s\n", ps->name);
  67. printf("ps.age=%d\n", ps->age);
  68. system("pause");
  69. return;
  70. }

结构体元素做函数参数pk结构指针做函数参数

define _CRT_SECURE_NO_WARNINGS

include

include

include

/**

定义一个结构体类型

定义了一个数据类型。固定大小内存块的别名 还没有分配内存

类型的重定义 **/

//用类型定义变量的方法3种 typedef struct Teacher { char name[64]; int age; int id; }Teacher;

//2 struct Student { char name[64]; int age; }s1, s2; // 定义类型的同时 D

//3 struct { char name[64]; int age; int id; }s3, s4; // 匿名类型 定义变量

//初始化变量的三种方法 2 struct Student2 { char name[64]; int age; }s5 = { “aaa”,10 };

//初始化变量的三种方法 3 struct { char name[64]; int age; }s6 = { “aaa”,10 };

void struct1() {

  1. //初始化变量的三种方法
  2. //struct Teacher t1;//告诉C编译器给我配分内存
  3. Teacher t1;//告诉C编译器给我配分内存 前面已经用typedef重定义了 所以struct可以去掉
  4. Teacher t2 = { "aaaa", 30, 01 };
  5. t1.age = 12; // t1. 操作符是干什么 有没有操作内存 -> 是寻址操作 计算 age 相对于 t1大变量的 偏移量 ===》计算 cpu中进行 没有操作操作内存
  6. //通过指针操作的方式 操作 内存空间
  7. {
  8. Teacher *p = NULL;
  9. p = &t2;
  10. printf("p->age:%d\n", p->age); // 是寻址操作 计算 cpu中进行 没有操作操作内存
  11. }
  12. strcpy(t1.name, "t1name");
  13. printf("t1.name:%s \n", t1.name);
  14. return;

}

void copyTeacher(Teacher to, Teacher from) { to = from; // 这里不会改变实参 }

void copyTeacher2(Teacher to, Teacher from) { to = from; }

void main() {

// struct1();

  1. Teacher t3 = { "Hello",18,22 };
  2. Teacher t4;
  3. Teacher t5;
  4. Teacher t6;
  5. memset(&t5, 0, sizeof(t5));
  6. t4 = t3;
  7. printf("t4.name:%s \n", t4.name); // hello
  8. printf("t4.age:%d \n", t4.age); // 18
  9. printf("copyTeacher()\n\n");
  10. copyTeacher(t5, t3); // 形参不会改变实参
  11. printf("t5.name:%s \n", t5.name); // ''
  12. printf("t5.age:%d \n", t5.age); // 0
  13. printf("copyTeacher2()\n\n");
  14. copyTeacher2(&t6, &t3); // 形参会改变实参
  15. printf("t6.name:%s \n", t6.name); // hello
  16. printf("t6.age:%d \n", t6.age); // 18
  17. system("pause");
  18. return;

}