繁体   English   中英

具有指针和结构的分段错误

[英]Segmentation Fault with Pointers and Structures

该程序应该用来操纵学生名单。 每次我尝试添加多个学生时,都会出现细分错误错误。 另外,当我尝试递归打印列表时,出现分段错误错误。 我认为这与我保存到结构和/或调用它的方式有关。 我对编程非常陌生,因此我确信它很简单。 有任何想法吗?

//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);
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM