C指向结构的指针问题-“ *”在结构变量之前是什么意思?

问题描述 投票:0回答:1

下面有一个简单的代码,询问姓氏,并分别为5个人提供​​2个等级。它也是查找所有年级的均值和谁的年级更高。问题与“ *”符号有关。

我的教授正在调用一个称为readstudent(Tstudent * pstu)的函数;pstu之前的*是什么意思,为什么有必要?

同样,当我们阅读readstudent(Tstudent * pstu)时,为什么1年级和2年级使用“&”,而名称中没有“&”?

#include <stdio.h>
#include <string.h>
typedef struct student
{
    char name[20];
    float grade1;
    float grade2;
} TStudent;



void readstudent( TStudent *pstu );
void printstudent( TStudent stu );
int main( void )
{
int N = 5;
        TStudent a[N]; int i, imax; float max, mo, sum;
    for(i=0; i<N; i++)
        readstudent( &a[i] );
        printf("\n Oi karteles twn foitntwv einai:\n");
    for(i=0; i<N; i++)
        printstudent( a[i]);
        sum = 0;
    for(i=0; i<N; i++)
        sum = sum + (a[i].grade1+a[i].grade2)/2;
        mo = (float)sum / N;
        printf("\nO mesos oros bathmologias tns taksns einai %2.2f\n", mo);
        imax = 0;
        max = (a[0].grade1+a[0].grade2)/2;
    for(i=0; i<N; i++)
        if ((a[i].grade1+a[i].grade2)/2 > max)
        {   
            max = (a[i].grade1+a[i].grade2)/2;
            imax = i;
        }
printf("\nO/H foitntns/tria me ton ypsnlotero meso oro (%4.2f) einai o/h %s.\n", max, a[imax].name);
return 0;
}

void readstudent( TStudent *pstu)
{
printf("Eisagwgh foitntn/trias: epwnymo <keno> bathmos1 <keno> bathmos2 <keno>: \n");
    scanf("%s", pstu->name);
    scanf("%f", &pstu->grade1);
    scanf("%f", &pstu->grade2);
}
void printstudent( TStudent stu)
{
    printf("Epwnymo: %s\n", stu.name);
    printf("Bathmos-1: %4.2f\n", stu.grade1);
    printf("Bathmos-2: %4.2f\n", stu.grade2);
}

感谢您的时间,感谢您的帮助!

c pointers struct
1个回答
0
投票

*在不同的上下文中表示不同的事物。请记住,C ++是一种上下文敏感语言。

声明变量时,例如;int* foo;*表示foo是“指向int的指针”。

[当您编写类似std::cout << *foo;的语句时,*表示dereference存储在foo中的指针,并给我它指向的int值。

[在像void f(int* bar)这样的函数声明中使用时,意味着该函数接受指向整数的指针作为其参数。

© www.soinside.com 2019 - 2024. All rights reserved.