在本教程中,我们将学习在 C 编程语言中使用带有结构体变量数组的指针因此,在之前的教程中,我们学习了如何为结构体变量创建指针现在让我们继续创建一个结构变量数组并通过指针变量使用它创建结构体变量数组在下面的示例中,我们考虑student在上一个教程中创建的结构,并创建一个std大小为 3 的学生结构变量数组来保存三个学生的详细信息// student structurestruct student { char id[15]; char firstname[64]; char lastname[64]; float points;};// student structure variablestruct student std[3];我们可以将std数组变量表示如下为结构体创建指针变量现在我们将创建一个指针变量,它将保存学生结构变量 std 的起始地址// student structure pointer variablestruct student *ptr = NULL;// assign std to ptrptr = std;笔记!std是一个数组变量,数组变量的名称指向内存位置,因此,我们将其分配给结构体指针变量ptr。
通过指针访问结构体数组变量的每个元素为此,我们首先将指针变量设置ptr为指向std变量的起始内存位置为此我们写了ptr = std;.然后,我们可以使用增量运算符来递增指针变量ptr++,使指针指向结构体数组变量的下一个元素,即从 str[0] 到 str[1]因为有三个学生,所以我们将循环三遍因此,我们将指针变量递增两次第一个增量将指针ptr从 std[0] 移动到 std[1],第二个增量将指针ptr从 std[1] 移动到 std[2]为了重置指针变量ptr以指向std我们编写的结构变量的起始内存位置ptr = std;完整代码#include 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", (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("Detail of student #%d", (i + 1)); // display result via std variable printf("Result via std"); printf("ID: %s", std[i].id); printf("First Name: %s", std[i].firstname); printf("Last Name: %s", std[i].lastname); printf("Points: %f", std[i].points); // display result via ptr variable printf("Result via ptr"); printf("ID: %s", ptr->id); printf("First Name: %s", ptr->firstname); printf("Last Name: %s", ptr->lastname); printf("Points: %f", ptr->points); // update pointer to point at next element // of the array std ptr++; } return 0;}输出:Enter detail of student #1Enter ID: s01Enter first name: YusufEnter last name: ShakeelEnter Points: 8Enter detail of student #2Enter ID: s02Enter first name: JaneEnter last name: DoeEnter Points: 9Enter detail of student #3Enter ID: s03Enter first name: JohnEnter last name: DoeEnter Points: 6Detail of student #1Result via stdID: s01First Name: YusufLast Name: ShakeelPoints: 8.000000Result via ptrID: s01First Name: YusufLast Name: ShakeelPoints: 8.000000Detail of student #2Result via stdID: s02First Name: JaneLast Name: DoePoints: 9.000000Result via ptrID: s02First Name: JaneLast Name: DoePoints: 9.000000Detail of student #3Result via stdID: s03First Name: JohnLast Name: DoePoints: 6.000000Result via ptrID: s03First Name: JohnLast Name: DoePoints: 6.000000我们可以std如下表示内存中的数组变量。
注意事项!每个学生数据占用 147 字节的内存成员数据类型尺寸ID字符15字节名字符64字节姓字符64字节点漂浮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]要通过指针访问结构体的成员,我们使用->箭头运算符。