C 语言中的结构体数组
在 C 语言编程中,struct 关键字用于定义派生数据类型。定义后,您可以声明一个结构体变量数组,就像声明一个 int、float 或 char 类型的数组一样。结构体数组有很多用途,例如存储类似于数据库表的记录,其中每行都具有不同的数据类型。
通常,结构体类型在代码开头定义,以便其类型可以在任何函数中使用。您可以声明一个结构体数组,稍后再在其中填充数据,也可以在声明时初始化它。
初始化结构体数组
我们定义一个名为 book 的结构体类型,如下所示 -
struct book{ char title[10]; double price; int pages; };
在程序运行过程中,您可以声明一个数组,并通过在花括号中赋值每个元素来初始化它。结构体数组中的每个元素本身就是一个结构体值。因此,我们有如下所示的嵌套花括号 -
struct book b[3] = { {"Learn C", 650.50, 325}, {"C Pointers", 175, 225}, {"C Pearls", 250, 250} };
编译器如何为这个数组分配内存? 由于我们有一个包含三个元素的struct数组,其大小为 32 字节,因此该数组占用"32 x 3"个字节。每个 32 字节的块将容纳"title"、"price"和"pages"元素。
L | E | A | R | N | C | 675.50 | 325 | ||||
C | P | O | I | N | T | E | R | S | 175 | 225 | |
C | P | E | A | R | L | S | 250 | 250 |
声明结构体数组
您也可以声明一个空的结构体数组。之后,您可以使用 scanf() 语句读取其中的数据,或者为每个元素赋值,如下所示 -
struct book b[3]; strcpy(b[0].title, "Learn C"); b[0].price = 650.50; b[0].pages=325; strcpy(b[1].title, "C Pointers"); b[1].price = 175; b[1].pages=225; strcpy(b[2].title, "C Pearls"); b[2].price = 250;250 b[2].pages=325;
读取结构体数组
我们也可以接受用户的数据来填充数组。
示例 1
在下面的代码中,使用 for 循环来接收数组中每个结构体元素的"title"、"price"和"pages"元素的输入。
#include <stdio.h> struct book{ char title[10]; double price; int pages; }; int main(){ struct book b[3]; strcpy(b[0].title, "Learn C"); b[0].price = 650.50; b[0].pages = 325; strcpy(b[1].title, "C Pointers"); b[1].price = 175; b[1].pages = 225; strcpy(b[2].title, "C Pearls"); b[2].price = 250; b[2].pages = 325; printf(" List of Books: "); for (int i = 0; i < 3; i++){ printf("Title: %s Price: %7.2lf Pages: %d ", b[i].title, b[i].price, b[i].pages); } return 0; }
输出
运行此代码时,将产生以下输出 -
List of Books: Title: Learn C Price: 650.50 Pages: 325 Title: C Pointers Price: 175.00 Pages: 225 Title: C Pearls Price: 250.00 Pages: 325
示例 2
本例中,定义了一个名为 student 的结构体类型。其元素包括"姓名";物理、化学和数学成绩;以及"百分比"。
声明了一个包含三个结构体学生类型的数组,并使用 for 循环由用户输入填充前四个元素。在循环内部,计算每个下标的"百分比"元素。
最后,打印一个包含学生姓名、成绩和百分比的数组,以显示成绩列表。
#include <stdio.h> struct student{ char name[10]; int physics, chemistry, maths; double percent; }; int main(){ struct student s[3]; strcpy(s[0].name, "Ravi"); s[0].physics = 50; s[0].chemistry = 60; s[0].maths = 70; strcpy(s[1].name, "Kiran"); s[1].physics = 55; s[1].chemistry = 66; s[1].maths = 77; strcpy(s[2].name, "Anil"); s[2].physics = 45; s[2].chemistry = 55; s[2].maths = 65; for (int i = 0; i < 3; i++){ s[i].percent = (double)(s[i].physics + s[i].maths + s[i].chemistry)/3; } printf(" Name Physics Chemistry \Maths Percent "); for(int i = 0; i < 3; i++){ printf("%s %d %d %d %5.2lf ", s[i].name, s[i].physics, s[i].chemistry, s[i].maths, s[i].percent); } return 0; }
输出
运行此代码时,将产生以下输出 -
Name Physics Chemistry Maths Percent Ravi 50 60 70 60.00 Kiran 55 66 77 66.00 Anil 45 55 65 55.00
对结构体数组进行排序
我们再举一个结构体数组的例子。这里,我们将使用冒泡排序技术,对"book"结构体类型的数组按价格升序排序。
注意:可以使用赋值运算符将一个结构体变量的元素直接赋值给另一个结构体变量。
示例
查看示例 -
#include <stdio.h> struct book{ char title[15]; double price; int pages; }; int main(){ struct book b[3] = { {"Learn C", 650.50, 325}, {"C Pointers", 175, 225}, {"C Pearls", 250, 250} }; int i, j; struct book temp; for(i = 0; i < 2; i++){ for(j = i; j < 3; j++){ if (b[i].price > b[j].price){ temp = b[i]; b[i] = b[j]; b[j] = temp; } } } printf(" List of Books in Ascending Order of Price: "); for (i = 0; i < 3; i++){ printf("Title: %s Price: %7.2lf Pages: %d ", b[i].title, b[i].price, b[i].pages); } return 0; }
输出
运行此代码时,将产生以下输出 -
List of Books in Ascending Order of Price: Title: C Pointers Price: 175.00 Pages: 225 Title: C Pearls Price: 250.00 Pages: 250 Title: Learn C Price: 650.50 Pages: 325
声明指向结构体数组的指针
我们也可以声明指向结构体数组的指针。C 语言使用间接运算符 () 来访问结构体变量的内部元素。
示例
以下示例展示了如何声明指向结构体数组的指针 -
#include <stdio.h> struct book { char title[15]; double price; int pages; }; int main(){ struct book b[3] = { {"Learn C", 650.50, 325}, {"C Pointers", 175, 225}, {"C Pearls", 250, 250} }; struct book *ptr = b; for(int i = 0; i < 3; i++){ printf("Title: %s Price: %7.2lf Pages: %d ", ptr -> title, ptr -> price, ptr -> pages); ptr++; } return 0; }
输出
运行此代码时,将产生以下输出 -
Title: Learn C Price: 650.50 Pages: 325 Title: C Pointers Price: 175.00 Pages: 225 Title: C Pearls Price: 250.00 Pages: 250