c - scanf 发出多个输入

标签 c struct int scanf

每当我尝试扫描输入时,例如:JASON BOURNE JULY 5 1972,程序就会崩溃。

scanf("%s %s %s %d %d", temp->fname, temp->lname, temp->month, temp->day, temp->year);

我确定它与 %d 有关,而如果我输入:

scanf("%s %s %s", temp->fname, temp->lname, temp->month);
scanf("%d %d", temp->month, temp->day);

String 值是正确的,程序在分配 int 之前崩溃了。

这是我的结构的副本:

typedef struct node{
    char fname [29];
    char lname [29];

    char month [9];
    int day;
    int year;

    struct student * next;
    struct student * previous;
} student;

这是我在 main() 中的函数的副本:

    student * head = malloc(sizeof(student));
    student * temp = head;
    int num = numStudents;
    while(num != 0){
        scanf("%s %s %s %d %d", temp->fname, temp->lname, temp->month, temp->day, temp->year);
        printf("FUNCTION NEVER REACHES THIS POINT");
        temp = temp->next = malloc(sizeof(student));
        num--;
    }temp->next = NULL;

最佳答案

如果有人感兴趣的话,这就是我为让一切正常工作所做的事情:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

//Struct for Student
struct student{
    char fname [29];
    char lname [29];

    char month [9];
    int day;
    int year;

    struct student *next;
    struct student *previous;
};

//Pre-Call
void printList(struct student *node);
struct student * insertNode(struct student * list, char fname[29], char lname[29], char month[9], int day, int year);


//Main function
int main(){
    //Take Input and Assign Variables
    int classes, i;
    scanf("%d", &classes);

    for(i = 0; i < classes; i++){
        int numStudents, j;
        scanf("%d", &numStudents);

        struct student *list = NULL; //Create LL
        int num = numStudents, day, year;
        char fname[29]; char lname[29]; char month[9];
        //Assign Values
        while(num != 0){
            scanf("%s %s %s %d %d", &fname, &lname, &month, &day, &year);
            list = insertNode(list, fname, lname, month, day, year);
            num--;
        }

        //

        printList(list);
    }


    return 0;
}


//Function to insert a node to the LL
struct student * insertNode(struct student * list, char fname[29], char lname[29], char month[9], int day, int year){
    //If the list is empty
    if(list == NULL){
        struct student * tempNode = (struct student *) malloc(sizeof(struct student));
        strcpy(tempNode->fname,fname);
        strcpy(tempNode->lname,lname);
        strcpy(tempNode->month,month);
        tempNode->day = day;
        tempNode->year = year;
        tempNode->next = NULL; // Extremely Important
        return tempNode;
    }
    //If the list has a node
    list->next = insertNode(list->next, fname, lname, month, day, year);
    return list;
}


//Function to print a LL
void printList(struct student * node){
    if(node->next == NULL)
        printf("%s %s %s %d %d\n", node->fname, node->lname, node->month, node->day, node->year);
    else
    {
        printf("%s %s %s %d %d\n", node->fname, node->lname, node->month, node->day, node->year);
        printList(node->next);
    }
}

关于c - scanf 发出多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42460241/

相关文章:

c - 使用C代码读取Ubuntu linux上的COM端口

python - 用 Python 打包数据

java - 负字符值JAVA

c - 如何同时调试C和Matlab代码?

c - 如何将用户定义的宏传递给 xcodebuild?

c - 将分配给堆栈和数组的大小。数组内存分配是在堆栈下还是单独分配

c++ - 如何将 24 位(整数值)转换为字符串,反之亦然?

Python 认为我的元组是一个整数

c - C 中的内存管理、在函数中传递指针、段错误

c++ - 尝试在 C++ 中使用结构时出错