C语言结构体数组
作者:--
发布时间:2019-11-20
评论:0
阅读:0
在c语言编程中可以将一系列结构体来存储不同数据类型的许多信息。 结构体数组也称为结构的集合。
我们来看一个数组结构体的例子,存储5位学生的信息并打印出来。创建一个源文件:structure-with-array.c,其代码实现如下 -
#include<stdio.h>
#include<conio.h>
#include<string.h>
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<max;i++) {
printf("rollno:%d, name:%s\n", st[i].rollno, st[i].name);
}
}
注:上面代码在工程: 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:刘建明