构造类型是C语言中用于组织复杂数据的重要工具,它允许我们将不同类型的数据组合在一起。东巴文(db-w.cn) 将带你深入理解C语言的构造类型。
💡 东巴文观点:构造类型是C语言处理复杂数据的基础,掌握构造类型是成为高级C程序员的必经之路。
#include <stdio.h>
void constructTypeDefinition() {
printf("=== 东巴文构造类型的定义 ===\n\n");
printf("构造类型:\n");
printf(" 由基本类型或其他构造类型组合而成的数据类型\n");
printf(" 用于表示复杂的数据结构\n\n");
printf("C语言的构造类型:\n");
printf(" 1. 数组类型(Array)\n");
printf(" 2. 结构体类型(Struct)\n");
printf(" 3. 共用体类型(Union)\n");
printf(" 4. 枚举类型(Enum)\n");
}
int main() {
constructTypeDefinition();
return 0;
}
#include <stdio.h>
void constructTypeFeatures() {
printf("=== 东巴文构造类型的特点 ===\n\n");
printf("构造类型的特点:\n");
printf(" 1. 可以包含多个成员\n");
printf(" 2. 成员可以是不同类型\n");
printf(" 3. 可以嵌套使用\n");
printf(" 4. 可以作为函数参数\n");
printf(" 5. 可以作为返回值\n");
}
int main() {
constructTypeFeatures();
return 0;
}
#include <stdio.h>
void oneDimensionalArray() {
printf("=== 东巴文一维数组 ===\n\n");
// 声明和初始化
int arr1[5] = {1, 2, 3, 4, 5};
int arr2[] = {10, 20, 30, 40, 50};
int arr3[5] = {0}; // 全部初始化为0
printf("arr1: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr1[i]);
}
printf("\n");
printf("arr2: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr2[i]);
}
printf("\n");
printf("arr3: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr3[i]);
}
printf("\n");
}
int main() {
oneDimensionalArray();
return 0;
}
#include <stdio.h>
void twoDimensionalArray() {
printf("=== 东巴文二维数组 ===\n\n");
// 声明和初始化
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
printf("二维数组:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%2d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
twoDimensionalArray();
return 0;
}
#include <stdio.h>
#include <string.h>
void characterArray() {
printf("=== 东巴文字符数组 ===\n\n");
// 字符数组
char str1[] = "Hello";
char str2[20] = "东巴文";
char str3[50];
strcpy(str3, "db-w.cn");
printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
printf("str3: %s\n", str3);
}
int main() {
characterArray();
return 0;
}
#include <stdio.h>
#include <string.h>
// 定义结构体
struct Student {
char name[50];
int age;
float score;
};
void structDefinition() {
printf("=== 东巴文结构体的定义 ===\n\n");
// 声明结构体变量
struct Student stu1;
struct Student stu2;
// 初始化
strcpy(stu1.name, "东巴文");
stu1.age = 20;
stu1.score = 95.5;
// 声明并初始化
struct Student stu3 = {"张三", 22, 88.0};
printf("stu1: %s, %d, %.1f\n", stu1.name, stu1.age, stu1.score);
printf("stu3: %s, %d, %.1f\n", stu3.name, stu3.age, stu3.score);
}
int main() {
structDefinition();
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct {
char title[100];
char author[50];
float price;
} Book;
void structUsage() {
printf("=== 东巴文结构体的使用 ===\n\n");
Book book1;
Book book2;
// 初始化
strcpy(book1.title, "C语言程序设计");
strcpy(book1.author, "东巴文");
book1.price = 59.9;
strcpy(book2.title, "数据结构");
strcpy(book2.author, "db-w.cn");
book2.price = 69.9;
printf("书籍1:\n");
printf(" 书名:%s\n", book1.title);
printf(" 作者:%s\n", book1.author);
printf(" 价格:%.2f\n", book1.price);
printf("\n书籍2:\n");
printf(" 书名:%s\n", book2.title);
printf(" 作者:%s\n", book2.author);
printf(" 价格:%.2f\n", book2.price);
}
int main() {
structUsage();
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct {
char name[50];
int age;
float score;
} Student;
void structArray() {
printf("=== 东巴文结构体数组 ===\n\n");
Student students[3] = {
{"张三", 20, 85.5},
{"李四", 21, 90.0},
{"王五", 22, 88.5}
};
printf("学生信息:\n");
for (int i = 0; i < 3; i++) {
printf("学生%d: %s, %d岁, 成绩%.1f\n",
i + 1, students[i].name, students[i].age, students[i].score);
}
}
int main() {
structArray();
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct {
char name[50];
int age;
} Person;
void structPointer() {
printf("=== 东巴文结构体指针 ===\n\n");
Person person = {"东巴文", 20};
Person *ptr = &person;
// 使用指针访问成员
printf("使用指针访问:\n");
printf(" 姓名:%s\n", ptr->name);
printf(" 年龄:%d\n", ptr->age);
// 使用指针修改成员
ptr->age = 21;
printf("\n修改后:\n");
printf(" 姓名:%s\n", person.name);
printf(" 年龄:%d\n", person.age);
}
int main() {
structPointer();
return 0;
}
#include <stdio.h>
// 定义共用体
union Data {
int i;
float f;
char str[20];
};
void unionDefinition() {
printf("=== 东巴文共用体的定义 ===\n\n");
union Data data;
printf("共用体大小:%zu字节\n", sizeof(data));
// 使用int成员
data.i = 100;
printf("\n使用int成员:\n");
printf(" data.i = %d\n", data.i);
// 使用float成员
data.f = 95.5;
printf("\n使用float成员:\n");
printf(" data.f = %.1f\n", data.f);
printf(" data.i = %d(已被覆盖)\n", data.i);
// 使用char数组成员
sprintf(data.str, "东巴文");
printf("\n使用char数组成员:\n");
printf(" data.str = %s\n", data.str);
printf(" data.i = %d(已被覆盖)\n", data.i);
printf(" data.f = %.1f(已被覆盖)\n", data.f);
}
int main() {
unionDefinition();
return 0;
}
#include <stdio.h>
typedef union {
int integer;
float floating;
char character;
} Value;
void unionFeatures() {
printf("=== 东巴文共用体的特点 ===\n\n");
Value value;
printf("共用体特点:\n");
printf(" 1. 所有成员共享同一内存空间\n");
printf(" 2. 大小等于最大成员的大小\n");
printf(" 3. 同一时刻只能使用一个成员\n");
printf(" 4. 修改一个成员会影响其他成员\n");
printf(" 5. 节省内存空间\n");
printf("\n共用体大小:%zu字节\n", sizeof(value));
printf("int大小:%zu字节\n", sizeof(int));
printf("float大小:%zu字节\n", sizeof(float));
printf("char大小:%zu字节\n", sizeof(char));
}
int main() {
unionFeatures();
return 0;
}
#include <stdio.h>
typedef struct {
int type; // 0: int, 1: float, 2: char
union {
int intValue;
float floatValue;
char charValue;
} value;
} Variant;
void unionApplication() {
printf("=== 东巴文共用体的应用 ===\n\n");
Variant variants[3];
// 存储int
variants[0].type = 0;
variants[0].value.intValue = 100;
// 存储float
variants[1].type = 1;
variants[1].value.floatValue = 95.5;
// 存储char
variants[2].type = 2;
variants[2].value.charValue = 'A';
printf("变体数据:\n");
for (int i = 0; i < 3; i++) {
printf("变体%d: ", i + 1);
switch (variants[i].type) {
case 0:
printf("int = %d\n", variants[i].value.intValue);
break;
case 1:
printf("float = %.1f\n", variants[i].value.floatValue);
break;
case 2:
printf("char = %c\n", variants[i].value.charValue);
break;
}
}
}
int main() {
unionApplication();
return 0;
}
#include <stdio.h>
// 定义枚举
enum Weekday {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
void enumDefinition() {
printf("=== 东巴文枚举的定义 ===\n\n");
enum Weekday today = WEDNESDAY;
printf("枚举值:\n");
printf(" SUNDAY = %d\n", SUNDAY);
printf(" MONDAY = %d\n", MONDAY);
printf(" TUESDAY = %d\n", TUESDAY);
printf(" WEDNESDAY = %d\n", WEDNESDAY);
printf(" THURSDAY = %d\n", THURSDAY);
printf(" FRIDAY = %d\n", FRIDAY);
printf(" SATURDAY = %d\n", SATURDAY);
printf("\n今天是星期%d\n", today + 1);
}
int main() {
enumDefinition();
return 0;
}
#include <stdio.h>
typedef enum {
RED = 1,
GREEN = 2,
BLUE = 3
} Color;
void enumFeatures() {
printf("=== 东巴文枚举的特点 ===\n\n");
Color color = GREEN;
printf("枚举特点:\n");
printf(" 1. 枚举值是整数常量\n");
printf(" 2. 默认从0开始\n");
printf(" 3. 可以指定值\n");
printf(" 4. 提高代码可读性\n");
printf(" 5. 便于维护\n");
printf("\n颜色值:\n");
printf(" RED = %d\n", RED);
printf(" GREEN = %d\n", GREEN);
printf(" BLUE = %d\n", BLUE);
printf("\n当前颜色:%d\n", color);
}
int main() {
enumFeatures();
return 0;
}
#include <stdio.h>
typedef enum {
STATUS_SUCCESS = 0,
STATUS_ERROR = -1,
STATUS_INVALID_PARAM = -2,
STATUS_OUT_OF_MEMORY = -3
} Status;
Status processData(int data) {
if (data < 0) {
return STATUS_INVALID_PARAM;
}
if (data > 100) {
return STATUS_ERROR;
}
return STATUS_SUCCESS;
}
void enumApplication() {
printf("=== 东巴文枚举的应用 ===\n\n");
int testData[] = {50, -10, 150};
for (int i = 0; i < 3; i++) {
Status status = processData(testData[i]);
printf("处理数据%d:", testData[i]);
switch (status) {
case STATUS_SUCCESS:
printf("成功\n");
break;
case STATUS_ERROR:
printf("错误\n");
break;
case STATUS_INVALID_PARAM:
printf("参数无效\n");
break;
case STATUS_OUT_OF_MEMORY:
printf("内存不足\n");
break;
}
}
}
int main() {
enumApplication();
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct {
int year;
int month;
int day;
} Date;
typedef struct {
char name[50];
Date birthday;
float score;
} StudentInfo;
void nestedStruct() {
printf("=== 东巴文结构体嵌套 ===\n\n");
StudentInfo student;
strcpy(student.name, "东巴文");
student.birthday.year = 2000;
student.birthday.month = 1;
student.birthday.day = 1;
student.score = 95.5;
printf("学生信息:\n");
printf(" 姓名:%s\n", student.name);
printf(" 生日:%d-%02d-%02d\n",
student.birthday.year,
student.birthday.month,
student.birthday.day);
printf(" 成绩:%.1f\n", student.score);
}
int main() {
nestedStruct();
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct {
char course[50];
float score;
} CourseScore;
typedef struct {
char name[50];
CourseScore courses[3];
float average;
} StudentRecord;
void arrayStructNesting() {
printf("=== 东巴文数组与结构体嵌套 ===\n\n");
StudentRecord student;
strcpy(student.name, "东巴文");
strcpy(student.courses[0].course, "数学");
student.courses[0].score = 95.0;
strcpy(student.courses[1].course, "英语");
student.courses[1].score = 88.5;
strcpy(student.courses[2].course, "物理");
student.courses[2].score = 92.0;
student.average = (student.courses[0].score +
student.courses[1].score +
student.courses[2].score) / 3;
printf("学生成绩单:\n");
printf(" 姓名:%s\n", student.name);
printf(" 各科成绩:\n");
for (int i = 0; i < 3; i++) {
printf(" %s: %.1f\n", student.courses[i].course, student.courses[i].score);
}
printf(" 平均分:%.1f\n", student.average);
}
int main() {
arrayStructNesting();
return 0;
}
#include <stdio.h>
void chooseConstructType() {
printf("=== 东巴文合理选择构造类型 ===\n\n");
printf("选择原则:\n");
printf(" 1. 数组:同类型数据集合\n");
printf(" 2. 结构体:不同类型数据组合\n");
printf(" 3. 共用体:节省内存,互斥数据\n");
printf(" 4. 枚举:有限个整数值\n");
}
int main() {
chooseConstructType();
return 0;
}
#include <stdio.h>
#include <string.h>
// 使用typedef
typedef struct {
char name[50];
int age;
} Person;
void useTypedef() {
printf("=== 东巴文使用typedef简化类型 ===\n\n");
Person person;
strcpy(person.name, "东巴文");
person.age = 20;
printf("typedef优势:\n");
printf(" 1. 简化类型声明\n");
printf(" 2. 提高可读性\n");
printf(" 3. 便于移植\n");
printf(" 4. 隐藏实现细节\n");
printf("\n人员信息:%s, %d岁\n", person.name, person.age);
}
int main() {
useTypedef();
return 0;
}
#include <stdio.h>
typedef struct {
char a;
int b;
char c;
} Struct1;
typedef struct {
int b;
char a;
char c;
} Struct2;
void memoryAlignment() {
printf("=== 东巴文注意内存对齐 ===\n\n");
printf("Struct1大小:%zu字节\n", sizeof(Struct1));
printf("Struct2大小:%zu字节\n", sizeof(Struct2));
printf("\n内存对齐原则:\n");
printf(" 1. 成员按其大小对齐\n");
printf(" 2. 结构体大小是最大成员大小的整数倍\n");
printf(" 3. 合理排列成员顺序\n");
printf(" 4. 节省内存空间\n");
}
int main() {
memoryAlignment();
return 0;
}
完成本章学习后,请确认:
掌握构造类型后,你可以继续学习:
如果遇到问题,欢迎访问 东巴文(db-w.cn) 获取帮助!
东巴文(db-w.cn) - 让编程学习更简单
🎯 东巴文构造类型提示:构造类型是C语言处理复杂数据的基础。在 db-w.cn,我们会通过大量实例帮你掌握构造类型!