C语言结构体(struct)用法详解(C语言结构体(struct)使用详解指南)
原创
一、结构体概述
在C语言中,结构体(struct)是一种用户自定义的数据类型,用于存储不同类型的数据项。结构体允许我们将多个变量组合成一个单一的复合变量。在结构体中,每个成员都可以有不同的数据类型。
二、结构体的定义
结构体的定义一般形式如下:
struct 结构体名称 {
数据类型 成员1;
数据类型 成员2;
...
};
例如,定义一个即学生的结构体:
struct Student {
int id;
char name[50];
float score;
};
三、结构体变量的声明
定义结构体后,我们可以使用该结构体类型声明变量。声明结构体变量的行为有以下几种:
struct Student stu1, stu2;
或者
struct Student {
int id;
char name[50];
float score;
} stu1, stu2;
或者使用typedef简化结构体类型声明:
typedef struct Student {
int id;
char name[50];
float score;
} StudentType;
StudentType stu1, stu2;
四、结构体成员的访问
结构体成员的访问使用点操作符(.),如下所示:
stu1.id = 1;
strcpy(stu1.name, "张三");
stu1.score = 90.5;
也可以通过指针访问结构体成员,使用箭头操作符(->):
struct Student *pStu = &stu1;
pStu->id = 1;
strcpy(pStu->name, "张三");
pStu->score = 90.5;
五、结构体数组
结构体数组是结构体变量的集合,用于存储多个结构体变量。声明结构体数组的行为如下:
struct Student stuArray[10];
结构体数组的初始化:
struct Student stuArray[2] = {
{1, "张三", 90.5},
{2, "李四", 85.0}
};
六、结构体指针
结构体指针是一个指向结构体类型的指针变量。声明结构体指针的行为如下:
struct Student *pStu;
通过结构体指针访问结构体成员:
pStu = &stu1;
pStu->id = 1;
strcpy(pStu->name, "张三");
pStu->score = 90.5;
七、结构体作为函数参数
结构体可以作为函数的参数传递,有以下几种行为:
- 传值行为:将结构体变量的值传递给函数形参
- 传址行为:将结构体变量的地址传递给函数形参
传值行为示例:
void printStudent(struct Student stu) {
printf("ID: %d ", stu.id);
printf("Name: %s ", stu.name);
printf("Score: %.2f ", stu.score);
}
int main() {
struct Student stu = {1, "张三", 90.5};
printStudent(stu);
return 0;
}
传址行为示例:
void printStudent(struct Student *pStu) {
printf("ID: %d ", pStu->id);
printf("Name: %s ", pStu->name);
printf("Score: %.2f ", pStu->score);
}
int main() {
struct Student stu = {1, "张三", 90.5};
printStudent(&stu);
return 0;
}
八、结构体与动态内存分配
在C语言中,可以使用malloc和free函数动态分配和释放结构体内存。以下是一个示例:
struct Student *pStu = (struct Student *)malloc(sizeof(struct Student));
if (pStu != NULL) {
pStu->id = 1;
strcpy(pStu->name, "张三");
pStu->score = 90.5;
// 使用结构体指针
free(pStu); // 释放内存
}
九、结构体的嵌套
结构体可以嵌套定义,即一个结构体可以作为另一个结构体的成员。以下是一个示例:
struct Date {
int year;
int month;
int day;
};
struct Student {
int id;
char name[50];
float score;
struct Date birthDate;
};
struct Student stu = {1, "张三", 90.5, {1999, 1, 1}};
十、结构体的应用实例
下面是一个使用结构体的易懂示例,该程序定义了一个学生管理系统,实现添加学生信息、打印学生信息等功能。
#include
#include
#include
struct Date {
int year;
int month;
int day;
};
struct Student {
int id;
char name[50];
float score;
struct Date birthDate;
};
void printStudent(struct Student *pStu) {
printf("ID: %d ", pStu->id);
printf("Name: %s ", pStu->name);
printf("Score: %.2f ", pStu->score);
printf("Birth Date: %d-%d-%d ", pStu->birthDate.year, pStu->birthDate.month, pStu->birthDate.day);
}
int main() {
struct Student stuArray[10];
int count = 0;
int choice;
while (1) {
printf("1. Add Student ");
printf("2. Print Student ");
printf("3. Exit ");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (count < 10) {
printf("Enter ID: ");
scanf("%d", &stuArray[count].id);
printf("Enter Name: ");
scanf("%s", stuArray[count].name);
printf("Enter Score: ");
scanf("%f", &stuArray[count].score);
printf("Enter Birth Date (YYYY MM DD): ");
scanf("%d %d %d", &stuArray[count].birthDate.year, &stuArray[count].birthDate.month, &stuArray[count].birthDate.day);
count++;
} else {
printf("Student array is full! ");
}
break;
case 2:
for (int i = 0; i < count; i++) {
printStudent(&stuArray[i]);
printf(" ");
}
break;
case 3:
exit(0);
default:
printf("Invalid choice! ");
}
}
return 0;
}