铜锣烧学电子 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 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持