C 带指针和结构的分段错误

C 带指针和结构的分段错误,c,pointers,struct,segmentation-fault,C,Pointers,Struct,Segmentation Fault,这个程序应该操作一个学生列表。每次我尝试添加多个学生时,都会出现分段错误。此外,当我尝试递归打印列表时,我会得到一个分段错误。我认为这与我如何保存结构和/或调用它有关。我对编程非常陌生,所以我相信它很简单。有什么想法吗 //Header file declarations. #include <stdio.h> #include <stdlib.h> #include <string.h> //Structure defintion. struct stud

这个程序应该操作一个学生列表。每次我尝试添加多个学生时,都会出现分段错误。此外,当我尝试递归打印列表时,我会得到一个分段错误。我认为这与我如何保存结构和/或调用它有关。我对编程非常陌生,所以我相信它很简单。有什么想法吗

//Header file declarations.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//Structure defintion.
struct student {
   int ID;
   char name[40];
   struct student *next;
};

//Type definition.
typedef struct student Student;

//Function prototypes.
int getChoice();
Student *addToList(Student *List);
void printList(Student *List);
void printListRR(Student *List);
void searchList(Student *List);

/*main function
Objective: This function provides runs a function call based on an option selected the user in another function.
Input: This function recieves no input from the user directly but it is passed their menu selection.
Output: The function outputs error messages and a closing salutation to the user. It returns 0.
*/ 
int main(void) {
    int choice = 0;
    Student *SLIST = NULL;

    //Call getChoice to get user's selection 
    choice = getChoice();

    //Switch-case for the possible menu selections 
    while(choice >= 0) {
        switch(choice) {
            case 0 : printf("Bye...\n"); exit(0);
            case 1 : SLIST = addToList(SLIST); break;
            case 2 : printList(SLIST); break;
            case 3 : printListRR(SLIST); break;
            case 4 : searchList(SLIST); break;
            default: printf("That is not a valid choice\n");
        }
        choice = getChoice();
    }

    if(SLIST) free(SLIST);
    return 0;
}

int getChoice() {
    int choice = 0;

    printf("\n****** MENU ******\n");
    printf("1. Add new student to list.\n");
    printf("2. Print the student list, beginning to end.\n");
    printf("3. Recursively print the student list from the end.\n");
    printf("4. Search the list for a student.\n");
    printf("0. Quit.\n");
    printf("\nEnter your choice: ");
    scanf("%d", &choice);

    return choice;
}

Student *addToList(Student *List){

    Student *studentPtr = (Student *) malloc(sizeof(Student));

    printf("Student ID: ");
    scanf("%d", &(studentPtr->ID));
    printf("Student Name: ");
    scanf(" %[^\n]", studentPtr->name);

    if(List == NULL){
        return studentPtr;
        }

    Student *nextStudent = List;

    while (nextStudent->next != NULL){
        nextStudent = nextStudent->next;
    }

    nextStudent->next = studentPtr;

    return List;
}

void printList(Student *List){
    while(List != NULL){
        printf("%d %s\n", List->ID, List->name);
        List = List->next;
    }
}

void printListRR(Student *List){
    if(List == NULL){
        return;
    }

    printListRR(List->next);
}

void searchList(Student *List){
    int idSearch;

    printf("Enter student ID to search for: ");
    scanf("%d", &idSearch);

    while(List != NULL){
        if(List->ID == idSearch){
            printf("%d %s\n", List->ID, List->name);
            return;
        }
        List = List->next;
    }
    printf("ID %d not found", idSearch);
}
//头文件声明。
#包括
#包括
#包括
//结构定义。
结构学生{
int-ID;
字符名[40];
结构学生*下一步;
};
//类型定义。
类型定义结构学生;
//功能原型。
int getChoice();
学生*地址列表(学生*列表);
作废打印列表(学生*列表);
作废打印列表RR(学生*列表);
无效搜索列表(学生*列表);
/*主要功能
目标:此函数提供根据用户在另一个函数中选择的选项运行函数调用。
输入:此功能不直接接收来自用户的输入,但会传递给他们的菜单选择。
输出:该函数向用户输出错误消息和结束问候语。它返回0。
*/ 
内部主(空){
int-choice=0;
Student*SLIST=NULL;
//调用getChoice以获取用户的选择
choice=getChoice();
//为可能的菜单选择切换外壳
而(选项>=0){
开关(选择){
案例0:printf(“Bye…\n”);退出(0);
案例1:SLIST=addToList(SLIST);中断;
案例2:打印列表(SLIST);中断;
案例3:printListRR(SLIST);中断;
案例4:搜索列表(SLIST);中断;
默认值:printf(“该选项无效”);
}
choice=getChoice();
}
如果(滑动)自由(滑动);
返回0;
}
int getChoice(){
int-choice=0;
printf(“\n*******菜单******\n”);
printf(“1.将新学生添加到列表中。\n”);
printf(“2.打印学生名单,从开始到结束。\n”);
printf(“3.从末尾递归打印学生列表。\n”);
printf(“4.在列表中搜索学生。\n”);
printf(“0.退出。\n”);
printf(“\n输入您的选择:”);
scanf(“%d”,选择(&C);
回报选择;
}
学生*地址列表(学生*列表){
学生*studentPtr=(学生*)malloc(学生人数);
printf(“学生ID:”);
scanf(“%d”和(studentPtr->ID));
printf(“学生姓名:”);
scanf(“%[^\n]”,studentPtr->name);
if(List==NULL){
返回学生PTR;
}
学生*nextStudent=列表;
while(nextStudent->next!=NULL){
nextStudent=nextStudent->next;
}
nextStudent->next=studentPtr;
退货清单;
}
作废打印列表(学生*列表){
while(List!=NULL){
printf(“%d%s\n”,列表->ID,列表->名称);
列表=列表->下一步;
}
}
作废打印列表RR(学生*列表){
if(List==NULL){
返回;
}
printListRR(列表->下一步);
}
无效搜索列表(学生*列表){
国际情报研究;
printf(“输入要搜索的学生ID:”);
scanf(“%d”和&idSearch);
while(List!=NULL){
如果(列表->ID==idSearch){
printf(“%d%s\n”,列表->ID,列表->名称);
返回;
}
列表=列表->下一步;
}
printf(“未找到ID%d”,idSearch);
}

尝试在addToList()中初始化studentPtr->next to NULL?

尝试在addToList()中初始化studentPtr->next to NULL?

使用
-g
选项编译程序并在调试器下运行。当程序崩溃时,调试器将告诉您是哪行代码导致了崩溃。您还可以探索崩溃时所有变量的值。希望这些信息能帮助您回答问题。当您创建一个新的列表条目(通过
malloc
)时,您需要将
next
元素初始化为
NULL
studentPtr->next=NULL。使用
-g
选项编译程序并在调试器下运行。当程序崩溃时,调试器将告诉您是哪行代码导致了崩溃。您还可以探索崩溃时所有变量的值。希望这些信息能帮助您回答问题。当您创建一个新的列表条目(通过
malloc
)时,您需要将
next
元素初始化为
NULL
studentPtr->next=NULL在学生姓名(未选中)扫描后。