在C語言編程中可以將一系列結(jié)構(gòu)體來存儲(chǔ)不同數(shù)據(jù)類型的許多信息。 結(jié)構(gòu)體數(shù)組也稱為結(jié)構(gòu)的集合。
我們來看一個(gè)數(shù)組結(jié)構(gòu)體的例子,存儲(chǔ)5位學(xué)生的信息并打印出來。創(chuàng)建一個(gè)源文件:structure-with-array.c,其代碼實(shí)現(xiàn)如下 -
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student {
int rollno;
char name[10];
};
// 定義可存儲(chǔ)的最大學(xué)生數(shù)量
#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<MAX;i++) {
printf("Rollno:%d, Name:%s\n", st[i].rollno, st[i].name);
}
}
注:上面代碼在工程: structure 下找到。
執(zhí)行上面示例代碼,得到以下結(jié)果 -
Enter Records of 3 students
Enter Rollno:1001
Enter Name:李明
Enter Rollno:1002
Enter Name:張會(huì)
Enter Rollno:1003
Enter Name:劉建明
Student Information List:
Rollno:1001, Name:李明
Rollno:1002, Name:張會(huì)
Rollno:1003, Name:劉建明