自定义数据结构与类型定义:C语言编程指南
1. 结构体(Structures)
在C语言中,结构体是一种用户自定义的数据类型,允许将不同类型的数据组合在一起,形成一个整体。结构体的定义和使用非常灵活,广泛应用于需要存储和处理复杂数据的应用场景中。下面是一个简单的结构体定义和使用示例:
1.1 定义结构体
#include<stdio.h>
struct Person {
int age;
char firstName[15];
char lastName[15];
char favoriteColour[10];
};
void PrintPerson(struct Person p) {
printf("First Name: %s\n", p.firstName);
printf("Last Name: %s\n", p.lastName);
printf("Favorite colour: %s\n", p.favoriteColour);
printf("Age: %d\n", p.age);
}
int main() {
struct Person p1;
p1.age = 10;
strcpy(p1.firstName, "John");
strcpy(p1.lastName, "Doe");
strcpy(p1.favoriteColour, "Red");
PrintPerson(p1);
struct Person p2;
p2.age = 25;