louiset 2015-09-09 07:59 采纳率: 57.1%
浏览 1805
已采纳

请问一下这个c程序有什么问题

题目:有n=3个结构体变量,内含学生学号,姓名和3门课程的成绩。要求输出平均成绩最高的学生的信息。

#include <stdio.h>
#define n 3
struct student
{long int number;
 char name[20];
 float s1,s2,s3;
 float average; 
};
int main()
{void input(struct student *p);
 struct student max(struct student *p);
 void print(struct student p);
 struct student stu[n],*p=stu;
 input(p);
 print(max(p));
 return 0;
}
void input(struct student *p)
{int i;
 for(i=0;i<n;i++)
 {printf("enter student%d's number,name,3 scores:",i+1);
  scanf("%ld%s%f%f%f",&*(p+i).number,*(p+i).name,&*(p+i).s1,&*(p+i).s2,&*(p+i).s3);
  *(p+i).average=(*(p+i).s1+*(p+i).s2+*(p+i).s3)/3.0;
 }

}
 struct student max(struct student *p)
 {int i,max;
  max=0;
  for(i=1;i<n;i++)
      if(*(p+max).average<*(p+i).average) max=i;
  return *(p+max);

 }
 void print(struct student p)
 {
    printf("number=&ld,name=%s,score1=%f,score2=%f,score3=%f",p.number,p.name,p.s1,p.s2,p.s3); 
 } 

--------------------Configuration: 6 - Win32 Debug--------------------
Compiling...
66.cpp
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(22) : error C2228: left of '.number' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(22) : error C2228: left of '.name' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(22) : error C2228: left of '.s1' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(22) : error C2228: left of '.s2' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(22) : error C2228: left of '.s3' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(23) : error C2228: left of '.average' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(23) : error C2228: left of '.s1' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(23) : error C2228: left of '.s2' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(23) : error C2228: left of '.s3' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(31) : error C2228: left of '.average' must have class/struct/union type
D:\vc 6.0\Microsoft Visual Studio\MyProjects\6\66.cpp(31) : error C2228: left of '.average' must have class/struct/union type
执行 cl.exe 时出错.

  • 写回答

3条回答 默认 最新

  • threenewbee 2015-09-09 08:12
    关注

    指针访问成员用->
    对象用.
    类用::

    另外
    struct student
    严格来说,这是C++的写法,C要用typedef。不过你其实用的是VC++,cpp,其实你写的是C++

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度