当我运行下面的代码来查找最大识字率和最大收入时,程序正在正确输入,但最后在显示最后两个 printf 语句的输出时,我收到以下错误“segmentation fault.core dumped”。请解释什么是错的..提前谢谢。
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
struct state_det {
char name[50];
long population;
float literacy;
long income;
}s[MAX];
int main()
{
int n,i,max_lit = 0, max_income = 0;
int t, p;
printf("enter number of states\n");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
printf("enter the name of the state %d\n",i);
scanf("%s",&s[i].name);
printf("enter the population of the state %d\n",i);
scanf("%ld",&s[i].population);
printf("enter the literacy rate of the state %d\n",i);
scanf("%f",&s[i].literacy);
printf("enter the average income of the state %d\n",i);
scanf("%ld",&s[i].income);
}
max_lit = s[0].literacy;
max_income = s[0].income;
for(i = 1; i < n; i++)
{
if(max_lit < s[i].literacy) {
max_lit = s[i].literacy;
t = i;
}
if(max_income < s[i].income) {
max_income = s[i].income;
p = i;
}
}
printf("\nthe state with highest literacy is %s and rate = %f\n",s[t].name, s[t].literacy);
printf("\nthe state with highest income is %s and rate = %ld\n",s[p].name, s[p].income);
return 0;
}