C语言 指针 - 分段错误(核心转储)



我很难理解我在这里做错了什么。我正在制作一个程序来组织一个书籍数据库。我正在使用链表将书籍结构保存到内存中。在输入书籍信息时,我得到

分段故障(核心转储)

输入第一个值后,书籍标识符。在我的AddBook函数中,我创建了一个临时书籍结构(aBook)。然后,我要求用户输入新书的标识符,然后尝试将其保存到aBook的标识符属性中。这是发生错误的地方。

无论如何,我包含了我的程序的顶部,其中包括库、声明函数等,我还包含了菜单函数和 addbook 函数,所以希望有人能发现我的错误是什么。提前谢谢。

我的代码的顶部:

//Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//only 10 books maximum
#define MAX 10;
//function prototypes
void fileInput();
void menuSystem();
int writeAndCloseFile();
void addBook();
void takeOutBook();
void returnBook();
void deleteBook();
void viewAllBooks();
void viewBook();
void viewYearCollection();
int exitSystem();
bool isEmpty();
//The File
FILE *fp;
//LinkedList Initialization
struct bookData {
    //book variables
    char* identifier;
    char* title;
    char* author;
    int year;
    bool status;
    char* customer;
    int timesTakenOut;
    char* genre;
};
//struct for one node
struct node {
    struct bookData *element;
    struct node *next;
};
//first and last nodes
struct node *firstBook = NULL;
struct node *lastBook = NULL;

菜单功能和添加书功能(发生错误):

//MENU SYSTEM FUNCTION
void menuSystem()
{
        int chosenOption = 0;
        printf("****************nn");
        printf("      MENUnn");
        printf("****************nn");
        printf("1 - Add bookn");
        printf("2 - Take out bookn");
        printf("3 - Return bookn");
        printf("4 - Delete bookn");
        printf("5 - View all booksn");
        printf("6 - View bookn");
        printf("7 - View Year Collectionn");
        printf("8 - Exitnn");
        printf("Chosen Option: ");
        scanf("%d", &chosenOption);
        //1. ADD BOOK
        if(chosenOption == 1)
        {
            addBook();
        }else if(chosenOption == 2){
        //2. TAKE OUT A BOOK
            takeOutBook();
        }else if(chosenOption == 3){
        //3. RETURN A BOOK
            returnBook();
        }else if(chosenOption == 4){
        //4. DELETE A BOOK
            deleteBook();
        }else if(chosenOption == 5){
        //5. VIEW ALL BOOKS
            viewAllBooks();
        }else if(chosenOption == 6){
        //6. VIEW A BOOK
            viewBook();
        }else if(chosenOption == 7){
        //7. VIEW YEAR COLLECTION
            viewYearCollection();
        }else if(chosenOption == 8){
        //8. EXIT SYSTEM
            printf("nnGoodbye!nnnn");
            exitSystem();
        }
    }

void addBook(){
    printf("n*** ADDING BOOKS ***n");
    struct node *aBookNode;
    struct bookData *aBook;
        aBook = (struct bookData *)malloc(sizeof(struct bookData));
        if (aBook == NULL)
            printf("Error - no space for new book datannn");
        else
        {
            //INPUT BOOK INFO
            //Identifier
            printf("nIdentifier(####-####): ");
            scanf("%9s", aBook->identifier);
            fflush(stdin);
            //Title
            printf("Title: ");
            scanf("%s", aBook->title);

控制台输出(在我输入标识符的随机数后):

Could not open the file book.dat
****************** The database is empty. Books will need to be manually entered ******************



*** ADDING BOOKS ***
Identifier(####-####): 1234-1234
Segmentation fault (core dumped)

您分配bookData结构,但不为结构内的字符串分配内存。

因此,当您要求scanf写入aBook->identifier时,它将写入一个看似随机的位置,最终导致未定义的行为和崩溃。

将那些应该是字符串的成员声明为固定大小的数组,或者为字符串分配内存。

scanf("%9s", aBook->identifier);

您需要为aBook->identifieraBook->title保留空间,一种简单的方法是使用 strdup(不是标准的一部分,但在许多实现中可用):

char temp[10];
scanf("%9s", temp);
aBook->identifier = strdup(temp);
if (aBook->identifier == NULL) { /* Always check the return of strdup */
    perror("strdup");
    exit(EXIT_FAILURE);
}

别忘了在最后打电话给free(aBook->identifier);

最新更新