结构体数组为何要在声明时定义

ZipperLin 2015-10-25 11:48:24

#include <stdio.h>
struct Student
{
int num;
char name[20];
float score;
int age;
};
void main()
{
struct Student a[50];
int temp, sum, num, times;
scanf("%d",&num);
for(temp = 1;temp <= num;temp++)
{
scanf("%d %s %f %d",&a[temp].num,a[temp].name,&a[temp].score,&a[temp].age);
}
for(times = 1;times <= num-1;times++) //冒泡排序
{
for(temp = 1;temp <= num-times;temp++)
{
if(a[temp].score>a[temp+1].score)
{
a[50] = a[temp];
a[temp] = a[temp+1];
a[temp+1] = a[50];
}
}
}
for(temp = 1;temp <= num;temp++)
{
printf("%d %s %g %d\n",a[temp].num,a[temp].name,a[temp].score,a[temp].age);
}
}

输入
3
101 张三 81 19
102 李四 60 18
103 王五 93 20
时运行
但是把数组换成在声明时定义就没事了
#include <stdio.h>
struct Student
{
int num;
char name[20];
float score;
int age;
}a[50];

这是为什么??
...全文
378 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
gydgzd 2019-07-02
  • 打赏
  • 举报
回复
a[50] = a[temp]; 越界了
linux_hsylar 2015-10-27
  • 打赏
  • 举报
回复
struct Student { int num; char name[20]; float score; int age; }; 这样定义后,使用是需要struct Student student [ 50]; 也就是说变量声明需用struct 结构体名 变量名。 改成 typedef struct Student { int num; char name[20]; float score; int age; }Student; 就没有上面问题了
苏叔叔 2015-10-25
  • 打赏
  • 举报
回复
另一种方法:

typedef struct Student
{
    int num; 
    char name[20];  
    float score; 
    int age;
}Student;
Student a[50];
toofunny 2015-10-25
  • 打赏
  • 举报
回复
1.越界 2.定义位置不同,越界造成的后果不同。 struct Student a[50]; 这是局部变量,越界访问影响到函数体。 struct Student { int num; char name[20]; float score; int age; }a[50]; 这是全局变量,越界访问未影响到函数体,会造成未知后果。
苏叔叔 2015-10-25
  • 打赏
  • 举报
回复
26行明显越界~
alohacsdn 2015-10-25
  • 打赏
  • 举报
回复
引用 5 楼 ZipperLin 的回复:
[quote=引用 3 楼 ahacsdn 的回复:]
struct Student a[50];

那么 a 的范围是 a[0]-a[49];
你排序的时候用a[50]做临时变量导致了越界。
你在声明了 a[50] 后,紧接着的声明:
int temp, sum, num, times;

这样 a[50] 所在的这块内存其实是这几个变量所在的内存区域,排序时由于给 a[50] 做了赋值,导致该内存区域中 temp和time的值改变,并未按照你的意愿在 for 循环中递增。

如果在结构声明后紧接着声明 a[50] ,两处内存就不是连在一起的,程序就正常跑了。
也不能说正常吧,越界总是危险的。
为什么排序时由于给 a[50] 做了赋值,会导致该内存区域中 temp和time的值改变?我调试的时候是正常的[/quote]


你把这几个变量的地址输出一下看看:


XDKHAN 2015-10-25
  • 打赏
  • 举报
回复
引用 6 楼 hshshaaa 的回复:
即便改成在结构体声明那里,也会报错。估计是编译器没有识别吧。我用的xcode,都会提示a[50]超过数组所包含的50个元素了。
我记得书上有这么一句话“系统不会检测数组元素的下标是否越界,编程时,必须保证数组下标不能越界”,这就能解释为什么编译没出问题,运行时出问题了。可能这是针对老一点的编程软件吧。
XDKHAN 2015-10-25
  • 打赏
  • 举报
回复


即便改成在结构体声明那里,也会报错。估计是编译器没有识别吧。我用的xcode,都会提示a[50]超过数组所包含的50个元素了。
ZipperLin 2015-10-25
  • 打赏
  • 举报
回复
引用 3 楼 ahacsdn 的回复:
struct Student a[50];
那么 a 的范围是 a[0]-a[49]; 你排序的时候用a[50]做临时变量导致了越界。 你在声明了 a[50] 后,紧接着的声明:
int temp, sum, num, times;
这样 a[50] 所在的这块内存其实是这几个变量所在的内存区域,排序时由于给 a[50] 做了赋值,导致该内存区域中 temp和time的值改变,并未按照你的意愿在 for 循环中递增。 如果在结构声明后紧接着声明 a[50] ,两处内存就不是连在一起的,程序就正常跑了。 也不能说正常吧,越界总是危险的。
为什么排序时由于给 a[50] 做了赋值,会导致该内存区域中 temp和time的值改变?我调试的时候是正常的
alohacsdn 2015-10-25
  • 打赏
  • 举报
回复
也不知道对不对,共同学习啊
alohacsdn 2015-10-25
  • 打赏
  • 举报
回复
struct Student a[50];
那么 a 的范围是 a[0]-a[49]; 你排序的时候用a[50]做临时变量导致了越界。 你在声明了 a[50] 后,紧接着的声明:
int temp, sum, num, times;
这样 a[50] 所在的这块内存其实是这几个变量所在的内存区域,排序时由于给 a[50] 做了赋值,导致该内存区域中 temp和time的值改变,并未按照你的意愿在 for 循环中递增。 如果在结构声明后紧接着声明 a[50] ,两处内存就不是连在一起的,程序就正常跑了。 也不能说正常吧,越界总是危险的。
ZipperLin 2015-10-25
  • 打赏
  • 举报
回复
引用 1 楼 zhangxiangDavaid 的回复:
另一种方法:

typedef struct Student
{
    int num; 
    char name[20];  
    float score; 
    int age;
}Student;
Student a[50];
问题是为什么第一种会错误

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧