C语言中的结构分割错误

C语言中的结构分割错误

您好,我正在尝试做一个简单的程序,将检查学生评估是否大于5.50。当我试图输入变量“number in class”时,我得到SEGFAULThere是我的代码:

#include <stdio.h>

typedef struct student{
    char name[100];
    int number_in_class;
    double assessment;
}student;

int main(void){
    student corrective[25];
    int i;
    for(i = 0; i < 25; i++){
        printf("Ime na uchenika:\n");
        scanf("%s", corrective[i].name);
        printf("Nomer v klas:\n");
        scanf("%i", corrective[i].number_in_class);
        printf("ocenka:\n");
        scanf("%f", corrective[i].assessment);
    }
    for(i = 0; i < 25; i++){
        if(corrective[i].assessment >= 5.50){
            printf("Ime: %s\nNomer v klas: %i\nOcenka: %i\n",corrective[i].name, corrective[i].number_in_class ,corrective[i].assessment);

        }

    }
    return 0;
}

知道为什么吗?

最佳答案

您需要将对象的地址传递给scanf(),而不是它们的值

    for(i = 0; i < 25; i++){
        printf("Ime na uchenika:\n");
        scanf("%s", corrective[i].name); // array converted to pointer to 1st element (an address)
        printf("Nomer v klas:\n");
        scanf("%i", &corrective[i].number_in_class); // use & to pass the address
        printf("ocenka:\n");
        scanf("%lf", &popravitelni[i].assessment); // same as before
    }

正如其他海报所指出的,您需要"%lf"来获取double类型对象的地址。

关于c - C语言中的结构分割错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22439870/

10-09 15:51