关于#c语言#的问题,请各位专家解答!段错误


#include<stdio.h>
struct worker{
    char name[10];
    double salary;
    double other;
    double cost;
    double end;
};
int main(){
    int n,i;
    struct worker pople[10];
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s %lf %lf %lf", &pople[i].name, &pople[i].salary, &pople[i].other, &pople[i].cost);
        pople[i].end=(pople[i].salary+pople[i].other)-pople[i].cost;
        
    }
    for(i=0;i<n;i++){
        printf("%s %.2f\n",pople[i].name,pople[i].end);
    }
   
    return 0;
}

img

//试试这样看行不行。不清楚你题目要求,看你的代码貌似n有点问题。


#include <stdio.h>
struct worker
{
    char name[10];
    double salary;
    double other;
    double cost;
    double end;
};
int main()
{
    int n, i;
    scanf("%d", &n);
    struct worker pople[n];
    for (i = 0; i < n; i++)
    {
        scanf("%s %lf %lf %lf", pople[i].name, &pople[i].salary, &pople[i].other, &pople[i].cost);
        pople[i].end = (pople[i].salary + pople[i].other) - pople[i].cost;
    }
    for (i = 0; i < n; i++)
    {
        printf("%s %.2lf\n", pople[i].name, pople[i].end);
    }
    return 0;
}



题目怎么要求的,你定义name长度为10,pople长度也是10,是否长度足够?

改动处见注释,供参考:

#include<stdio.h>
struct worker{
    char name[10];
    double salary;
    double other;
    double cost;
    double end;
};
int main(){
    int n,i;
    struct worker pople[10];
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s %lf %lf %lf", pople[i].name, &pople[i].salary, &pople[i].other, &pople[i].cost);//修改
        //scanf("%s %lf %lf %lf", &pople[i].name, &pople[i].salary, &pople[i].other, &pople[i].cost);
        pople[i].end=(pople[i].salary+pople[i].other)-pople[i].cost;

    }
    for(i=0;i<n;i++){
        printf("%s %.2f\n",pople[i].name,pople[i].end);
    }

    return 0;
}