C-指针和结构数组
在本教程中,我们将学习使用C编程语言将指针与结构变量数组一起使用。
因此,在上一教程中,我们学习了如何为结构变量创建指针。
现在让我们继续创建一个结构变量数组,并通过指针变量使用它。
创建一个结构变量数组
在下面的示例中,我们将考虑在上一教程中创建的"学生"结构,并创建一个大小为3的学生结构变量" std"数组,以容纳三个学生的详细信息。
//student structure
struct student {
char id[15];
char firstname[64];
char lastname[64];
float points;
};
//student structure variable
struct student std[3];
我们可以如下表示" std"数组变量。
创建结构的指针变量
现在,我们将创建一个指针变量,该变量将保存学生结构变量std的起始地址。
//student structure pointer variable struct student *ptr = NULL; //assign std to ptr ptr = std;
注意! " std"是一个数组变量,数组变量的名称指向内存位置,因此,我们将其分配给结构指针变量" ptr"。
通过指针访问结构数组变量的每个元素
为此,我们首先将指针变量" ptr"设置为指向" std"变量的起始存储位置。
为此,我们编写" ptr = std;"。
然后,我们可以使用增量运算符" ptr ++"来递增指针变量,以使指针指向结构数组变量的下一个元素,即从str [0]到str [1]。
因为有3个学生,所以我们将循环3次。
因此,我们将指针变量增加两次。
第一个增量会将指针" ptr"从std [0]移动到std [1],第二个增量会将指针" ptr"从std [1]移动到std [2]。
为了重置指针变量" ptr"以指向结构变量" std"的起始存储位置,我们编写" ptr = std;"。
完整的代码
#include <stdio.h>
int main(void) {
//student structure
struct student {
char id[15];
char firstname[64];
char lastname[64];
float points;
};
//student structure variable
struct student std[3];
//student structure pointer variable
struct student *ptr = NULL;
//other variables
int i;
//assign std to ptr
ptr = std;
//get detail for user
for (i = 0; i < 3; i++) {
printf("Enter detail of student #%d\n", (i + 1));
printf("Enter ID: ");
scanf("%s", ptr->id);
printf("Enter first name: ");
scanf("%s", ptr->firstname);
printf("Enter last name: ");
scanf("%s", ptr->lastname);
printf("Enter Points: ");
scanf("%f", &ptr->points);
//update pointer to point at next element
//of the array std
ptr++;
}
//reset pointer back to the starting
//address of std array
ptr = std;
for (i = 0; i < 3; i++) {
printf("\nDetail of student #%d\n", (i + 1));
//display result via std variable
printf("\nResult via std\n");
printf("ID: %s\n", std[i].id);
printf("First Name: %s\n", std[i].firstname);
printf("Last Name: %s\n", std[i].lastname);
printf("Points: %f\n", std[i].points);
//display result via ptr variable
printf("\nResult via ptr\n");
printf("ID: %s\n", ptr->id);
printf("First Name: %s\n", ptr->firstname);
printf("Last Name: %s\n", ptr->lastname);
printf("Points: %f\n", ptr->points);
//update pointer to point at next element
//of the array std
ptr++;
}
return 0;
}
Enter detail of student #1 Enter ID: s01 Enter first name: Enter last name: Enter Points: 8 Enter detail of student #2 Enter ID: s02 Enter first name: Jane Enter last name: Doe Enter Points: 9 Enter detail of student #3 Enter ID: s03 Enter first name: John Enter last name: Doe Enter Points: 6 Detail of student #1 Result via std ID: s01 First Name: Last Name: Points: 8.000000 Result via ptr ID: s01 First Name: Last Name: Points: 8.000000 Detail of student #2 Result via std ID: s02 First Name: Jane Last Name: Doe Points: 9.000000 Result via ptr ID: s02 First Name: Jane Last Name: Doe Points: 9.000000 Detail of student #3 Result via std ID: s03 First Name: John Last Name: Doe Points: 6.000000 Result via ptr ID: s03 First Name: John Last Name: Doe Points: 6.000000
我们可以如下所示在内存中表示" std"数组变量。
注意点!
每个学生数据占用147个字节的内存。
| 成员 | 数据类型 | 大小 |
|---|---|---|
| id | char | 15个字节 |
| 名字 | 字符 | 64字节 |
| 姓氏 | 字符 | 64字节 |
| points | 浮点数 | 4个字节 |
数组大小为3,因此,总共147x3,即441个字节分配给了" std"数组变量。
第一个元素" std [0]"的存储位置从1000到1146。
第二个元素" std [1]"从1147到1293获取内存位置。
第三个元素" std [2]"从1294到1440获得内存位置。
首先,使" ptr"指针变量指向地址1000,该地址是第一个元素" std [0]"的起始地址。
然后向前移动,我们增加指针" ptr ++",因此它指向存储位置1147,即第二个元素" std [1]"的起始存储位置。
同样,在下一次运行中,我们将" ptr"指向内存位置1294,即第三元素" std [2]"的起始位置。
要通过指针访问结构的成员,我们使用->箭头运算符。

