C/C++ - 结构体(struct)

目录

结构体的定义和声明

结构体成员的访问

结构体数组

结构体嵌套


结构体的定义和声明

  • 结构体是一种自定义数据类型,它允许你将不同类型的数据项组合成一个单独的实体

  • 结构体的定义包括结构体关键字struct​​,结构体名字和结构体成员

  • 结构体可以包含多个成员,这些成员可以是不同的数据类型

  • #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Person
    {
    	char Name[50];
    	int Age;
    	int Score;
    }p3;
    
    int main()
    {
    	//结构体变量定义与初始化
    	//数据类型 变量名 = 初始值;
    	//数据类型 数组名[元素个数] = {初始化列表};
    
    	//方式一
    	struct Person p1 = { 0 };
    	strcpy(p1.Name, "0xCC");
    	p1.Age = 18;
    	p1.Score = 100;
    
    	//方式二
    	struct Person p2 = {"0xCC", 18, 100};
    
    	//方式三
    	p3.Age = 25;
    	p3.Score = 80;
    
    	return 0;
    }
    

结构体成员的访问

  • 使用点运算符(.​​)来访问结构体成员

  • // 结构体成员的访问
    strcpy(p1.name, "John Doe");
    p1.age = 25;
    p1.height = 1.75;

结构体数组

  • 定义结构体

    • struct Person {
          char name[50];
          int age;
          float height;
      };
      
  • 声明结构体数组

    • struct Person people[3];
      
  • 初始化结构体数组

    • // 逐个为结构体数组元素赋值
      strcpy(people[0].name, "John Doe");
      people[0].age = 25;
      people[0].height = 1.75;
      
      strcpy(people[1].name, "Jane Smith");
      people[1].age = 30;
      people[1].height = 1.65;
      
      // 使用初始化列表进行初始化
      struct Person people[] = 
      {
          {"John Doe", 25, 1.75},
          {"Jane Smith", 30, 1.65},
          // 可以添加更多的结构体元素
      };
      
  • 访问结构体数组元素

    • printf("Name: %s\n", people[0].name);
      printf("Age: %d\n", people[0].age);
      printf("Height: %.2f\n", people[0].height);
      
  • 示例代码

    • #define _CRT_SECURE_NO_WARNINGS
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      #define MAX_LENGTH 50
      
      //1. 定义结构体类型
      struct Person
      {
      	char name[MAX_LENGTH];
      	int age;
      	int score;
      };
      
      int main()
      {
      	//结构体数组
      
      	//2. 声明结构体数组
      	struct Person people[3] = { 0 };
      	char* szName[3] = { "0xAA", "0xBB", "0xCC" };
      	
      	//3. 赋值结构体数组
      	for (int  i = 0; i < sizeof(people) / sizeof(people[0]); i++)
      	{
      		strcpy(people[i].name, szName[i]);
      		people[i].age = 18 + i;
      		people[i].score = 98 + i;
      	}
      
      	//4. 访问结构体元素
      	for (int i = 0; i < sizeof(people) / sizeof(people[0]); i++)
      	{
      		printf("Name -> [%s]  Age -> [%d] Score -> [%d] \r\n",
      			people[i].name,
      			people[i].age,
      			people[i].score);
      	}
      
      	return 0;
      }
      

结构体嵌套

  • 定义嵌套结构体 - 结构体可以嵌套在另一个结构体中,形成结构体的层次结构。这允许我们在一个结构体中使用另一个结构体作为其成员

    • struct Date {
          int day;
          int month;
          int year;
      };
      
      struct Person {
          char name[50];
          int age;
          struct Date birthday;
      };
      
  • 声明嵌套结构体变量 - 在使用嵌套结构体之前,我们需要声明一个嵌套结构体变量。可以使用点运算符(.​​)来访问嵌套结构体的成员

    • struct Person p1;
      
  • 初始化嵌套结构体 - 嵌套结构体可以通过逐个为每个成员赋值来进行初始化,也可以使用花括号初始化列表进行初始化

    • // 逐个为嵌套结构体成员赋值
      strcpy(p1.name, "John Doe");
      p1.age = 25;
      p1.birthday.day = 1;
      p1.birthday.month = 1;
      p1.birthday.year = 1990;
      
      // 使用初始化列表进行初始化
      struct Person p2 = {
          "Jane Smith",
          30,
          {15, 5, 1985}
      };
      
  • 访问嵌套结构体成员 - 可以使用点运算符(.)来访问嵌套结构体的成员

    • printf("Name: %s\n", p1.name);
      printf("Age: %d\n", p1.age);
      printf("Birthday: %d/%d/%d\n", p1.birthday.day, p1.birthday.month, p1.birthday.year);
      
  • 课堂代码

    • #define _CRT_SECURE_NO_WARNINGS
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #define MAX_LENGTH 50
      
      //1. 定义嵌套结构体
      
      struct Data
      {
      	int Year;
      	int Month;
      	int Day;
      };
      
      struct Person
      {
      	char name[MAX_LENGTH];
      	int age;
      	struct Data Birthday;
      };
      
      
      int main()
      {
      	//结构体嵌套(选择结构 - 循环结构)
      
      	//2. 声明嵌套结构体
      	//struct Person p1 = { 0 };
      	struct Person p1 =
      	{
      		 "0xCC",
      		 18,
      		{2005, 10, 1},
      	};
      
      	//3. 赋值嵌套结构体
      	strcpy(p1.name, "Tony");
      	p1.age = 18;
      	p1.Birthday.Year = 2000;
      	p1.Birthday.Month = 11;
      	p1.Birthday.Day = 11;
      
      	//4. 访问嵌套结构体
      	printf("Name -> [%s] \nAge -> [%d]\nBirthday->[%d/%d/%d]\r\n",
      		p1.name,
      		p1.age,
      		p1.Birthday.Year,
      		p1.Birthday.Month,
      		p1.Birthday.Day);
      
      
      	return 0;
      }
      
  • 结构体赋值 - 可以将一个结构体变量的值赋给另一个结构体变量。在赋值时,会将源结构体变量的每个成员的值逐个复制到目标结构体变量的对应成员

    • struct Person {
          char name[50];
          int age;
      };
      
      struct Person person1 = {"John Doe", 25};
      struct Person person2;
      
      person2 = person1;  // 将person1的值赋给person2
      
      printf("Name: %s\n", person2.name);  // 输出: "John Doe"
      printf("Age: %d\n", person2.age);    // 输出: 25
      
  • 结构体内存布局

    • #define _CRT_SECURE_NO_WARNINGS
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      struct Person
      {
      	char name[50];  //50Byte
      	int age;		//4Byte
      	int score;		//4Byte
      }/*size of 60*/;
      
      int main()
      {
      
      	printf("%d \r\n", sizeof(struct Person));
      	//结构体赋值
      	struct Person p1 = { "0xCC", 18, 100 };
      	struct Person p2 = p1;
      
      	return 0;
      }
      

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值