下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

C语言结构体数组定义(附实例)

作者:初生不惑     来源: http://www.kokojia.com点击数:3477发布时间: 2020-02-12 14:52:29

标签: 编程语言C语言课程

大神带你学编程,欢迎选课

C语言中,结构体(struct)指的是一种数据结构,是C语言中聚合数据类型(aggregate data type)的一类。结构体可以被声明为变量、指针或数组等,用以实现较复杂的数据结构。结构体同时也是一些元素的集合,这些元素称为结构体的成员(member),且这些成员可以为不同的类型,成员一般用名字访问。

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

}
C

注:上面代码在工程: structure 下找到。

执行上面示例代码,得到以下结果 -

shell code-toolbar">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:刘建明
其实,C++提供了许多种基本的数据类型(如int、float、double、char等)供用户使用。但是由于程序需要处理的问题往往比较复杂,而且呈多样化,已有的数据类型显得不能满足使用要求。
赞(10)
踩(1)
分享到:
华为认证网络工程师 HCIE直播课视频教程