C语言教程:结构体数组(集合)

2018-10-0819:04:12编程语言入门到精通Comments3,241 views字数 756阅读模式

C语言编程中可以将一系列结构体来存储不同数据类型的许多信息。 结构体数组也称为结构的集合。
我们来看一个数组结构体的例子,存储5位学生的信息并打印出来。创建一个源文件:structure-with-array.c,其代码实现如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6460.html

#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);
    }

}
C

注:上面代码在工程: structure 下找到。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6460.html

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6460.html

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:刘建明
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6460.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/6460.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定