使用箭头运算符计算结构中 3 个成员的平均值,需要从点运算符转换为箭头运算符

using arrow operator to calculate average of 3 members of a structure, need to convert from dot operator to arrow

我正在练习 c,我刚刚学会了如何分配整数和创建结构,我遇到了箭头运算符,但我不知道如何应用它,我研究了一下,我现在知道 a-> b 与 (*a).b 相同并且箭头用于指针,我的问题是如何将此代码转换为使用箭头运算符,我尝试将成员从 int 更改为 int * 但它仍然不起作用.

#include <stdio.h>
#include <string.h>
struct student {
    char name[10];
    int chem_marks;
    int maths_marks;
    int phy_marks;
};
int main()
{
struct student ahmad;
struct student ali;
struct student abu_abbas;

strcpy (ahmad.name,"ahmad");
ahmad.chem_marks=25;
ahmad.maths_marks=50;
ahmad.phy_marks=90;

strcpy (ali.name,"ali");
ali.chem_marks=29;
ali.maths_marks=90;
ali.phy_marks=13;

strcpy (abu_abbas.name,"abu");
abu_abbas.chem_marks=50;
abu_abbas.maths_marks=12;
abu_abbas.phy_marks=80;

int ahmadavg=(ahmad.chem_marks+ahmad.maths_marks+ahmad.phy_marks)/3;
int aliavg=(ali.chem_marks+ali.maths_marks+ali.phy_marks)/3;
int abu_abbasavg=(abu_abbas.chem_marks+abu_abbas.maths_marks+abu_abbas.phy_marks)/3;


printf("%s  ",ahmad.name);
printf("average:%d\n",ahmadavg);
printf("%s ",ali.name);
printf("average:%d\n",aliavg);
printf("%s ",abu_abbas.name);;
printf("average:%d\n",abu_abbasavg);


}

这与结构的成员是否为指针无关,而是关于结构与指向结构的指针的关系。

这个小例子应该说清楚了:

#include <stdio.h>

struct Foo
{
  int a;
  int b;
};

int main()
{
  struct Foo f = {1,2};    // f is a structre

  struct Foo* pf;          // pf is a pointer to a struct Foo
                           // it points nowhere

  pf = &f;                 // now pf points to f

  printf("%d %d\n", f.a, f.b);         // direct access to f

  printf("%d %d\n", pf->a, pf->b);     // access via a pointer
  printf("%d %d\n", (*pf).a, (*pf).b); // access via a pointer (same as line above)
}