在C语言编程中可以将一系列结构体来存储不同数据类型的许多信息。 结构体数组也称为结构的集合。
我们来看一个数组结构体的例子,存储5位学生的信息并打印出来。创建一个源文件:structure-with-array.c,其代码实现如下 –
#include #include #include struct student { int rollno; char name[10]; }; // 定义可存储的最大学生数量 #define MAX 3 void main() { int i; struct student st[MAX]; printf("Enter Records of 3 students"); for (i = 0;i < MAX;i++) { printf("nEnter Rollno:"); scanf("%d", &st[i].rollno); printf("Enter Name:"); scanf("%s", &st[i].name); } printf("Student Information List:n"); for (i = 0;i
注:上面代码在工程: structure 下找到。
执行上面示例代码,得到以下结果 –
Enter Records of 3 students Enter Rollno:1001 Enter Name:李明 Enter Rollno:1002 Enter Name:张会 Enter Rollno:1003 Enter Name:刘建明 Student Information List: Rollno:1001, Name:李明 Rollno:1002, Name:张会 Rollno:1003, Name:刘建明
¥ 我要打赏 纠错/补充 收藏