铜锣烧学电子 2022-04-14 22:36 采纳率: 78.6%
浏览 27
已结题

关于结构体的一个问题

问题遇到的现象和发生背景

运行报错

问题相关代码,请勿粘贴截图

#include<stdio.h>
struct Student
{
char num[6];
char name[10];
int score;
}std[10];
struct Student max(struct Student std[],int n)
{
int i,max=0;
for(i=1;i<n;i++)
{
if(std[max].score<std[i].score)
max=i;
}
return std[max];
}
void print(struct Student std)
{
printf("%s %s %d",std.num,std.name,std.score);
}
int main()
{
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s %s %d",std[i].num,std[i].name,&std[i].score);
print(max(std,n));
return 0;
}

运行结果及报错内容

[Error] expected primary-expression before '[' token
[Error] 'Student std [10]' redeclared as different kind of symbol

我的解答思路和尝试过的方法

我看不出来哪里错了
我改成这样就对了
#include<stdio.h>
struct Student
{
char num[6];
char name[10];
int score;
}stud[10];//改的地方
struct Student max(struct Student std[],int n)
{
int i,max=0;
for(i=1;i<n;i++)
{
if(std[max].score<std[i].score)
max=i;
}
return std[max];
}
void print(struct Student std)
{
printf("%s %s %d",std.num,std.name,std.score);
}
int main()
{
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s %s %d",stud[i].num,stud[i].name,&stud[i].score);//改的内容
print(max(stud,n));//改的内容
return 0;
}

我想要达到的结果

麻烦帮我指出问题,和解决方法。

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-04-15 08:48
    关注

    在c++ 里,std是“命名空间”标识符,“std”相当于已经被系统征用了,代码里把结构体数组定义为std[N] , 名字就冲突了,把数组定义改为stud[10]是正确的处理方式。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月23日
  • 已采纳回答 4月15日
  • 创建了问题 4月14日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分