简体   繁体   English

如何仅使用指针访问结构数组

[英]How to do I access an array of structs using ONLY pointers

I am trying to access the students array of structs and modify a "Student" at a given index (using a for loop).我正在尝试访问结构的学生数组并在给定索引处修改“学生”(使用 for 循环)。

However, I am trying to access it using only a pointer, not like this:但是,我试图只使用一个指针来访问它,而不是这样:
students[currStudent].theory = thoeryGrade;学生[currStudent].theory = thoeryGrade;

But something along the lines of:但是类似于以下内容:
*(studentPtr + currStudent).practical = practicalGrade; *(studentPtr + currStudent).practical = 实用等级;
Note: The above line doesn't compile.注意:上面的行不编译。

#include <stdlib.h>
#include <stdio.h>

typedef struct Student
{
    double theory;
    double practical;
} Student;

int main()
{
    Student students[50];
    const int numStudents;
    int thoeryGrade, practicalGrade = 0;
    Student *studentPtr = students;

    printf("Enter number of students: ");
    scanf("%d", &numStudents);

    while (1)
    {
        for (int currStudent = 0; currStudent < numStudents; currStudent++)
        {
            printf("Enter theory average of student %d: ", (currStudent + 1));
            scanf("%d", &thoeryGrade);

            printf("Enter practical average of student %d: ", (currStudent + 1));
            scanf("%d", &practicalGrade);

            students[currStudent].theory = thoeryGrade;
            *(studentPtr + currStudent).practical = practicalGrade;
            printf("\n");
        }
        for (int currStudent = 0; currStudent < numStudents; currStudent++)
        {
            printf("Student # %d -> Theory: %.2f", (currStudent + 1), students[currStudent].theory);
            printf("\n");
            printf("Student # %d -> Practical %.2f", (currStudent + 1), students[currStudent].practical);
            printf("\n\n");
        }
    }

    return 0;
}

The . . operator has higher precedence than * (dereference), so you need to enclose the expression *(studentPtr + currStudent) in brackets.运算符的优先级高于* (取消引用),因此您需要将表达式*(studentPtr + currStudent)括在括号中。

(*(studentPtr + currStudent)).practical = practicalGrade;

Alternatively, use the -> operator to directly access a member on the pointer.或者,使用->运算符直接访问指针上的成员。

(studentPtr + currStudent)->practical = practicalGrade;

This code has issues.这段代码有问题。

You must be ignoring compiler warnings about const in the declaration of const int numstudents .您必须在const int numstudents的声明中忽略关于const的编译器警告。

Why does the scanf() accept integers, then store the pair into doubles?为什么scanf()接受整数,然后将对存储为双精度数?

It may be safe(?) to disregard the 0th element of the array, and make the loops for input and printing:忽略数组的第 0 个元素可能是安全的(?),并为输入和打印创建循环:

for (int currStudent = 1; currStudent <= numStudents; currStudent++)

Then you wouldn't need the currStudent+1 excess baggage.那么你就不需要currStudent+1超重行李了。

The easiest way to condense the code is to use a temporary pointer inside the loop that points to the current student (array element).压缩代码的最简单方法是在循环内使用一个临时指针,该指针指向当前学生(数组元素)。

for (int currStudent = 1; currStudent <= numStudents; currStudent++) {
    Student *pCur = &students[ currStudent ];

Then you can use pCur->theory and pCur->practical to copy or print.然后您可以使用pCur->theorypCur->practical进行复制或打印。

Finally, "student" appears as all-or-part-of a datatype, an array name, a loop counter and text in a string.最后,“学生”显示为数据类型、数组名称、循环计数器和字符串中的文本的全部或部分。 Perhaps the datatype could be renamed 'record' and the loop counter 'index'.也许数据类型可以重命名为“记录”和循环计数器“索引”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM