列表插入中的strcmp出现分段错误

I'm trying to create a list in alphabetical order (first by author, then by name). I tried to do it by comparing the two strings with strcmp() but I'm getting a segmentation fault.

我将在这里留下GDB运行结果和函数,这是我使用列表制作的第一个程序之一,因此代码可能还有其他错误。

GDB运行:

Program received signal SIGSEGV, Segmentation fault.                         
0x0000000000400c65 in insert (lPtr=0x7fffffffeb38, isbn_i=978044,            
    name_i=0x7fffffffeb40 "Suzanne Collins",                                 
    author_i=0x7fffffffeb80 "The Hunger Games") at main.c:178                
178             while(strcmp(author_i, currPtr->author) < 0){
typedef struct library{ //struct insertion from stdin
    int isbn;
    char *author;
    char *name;
    int act; //actual books in memory
    int tot; //total books
    struct library *nextPtr; //node
}Lib;

typedef Lib *NodePtr;


//called by a while, receives input from integers and two arrays of char

void insert(NodePtr *lPtr, int isbn_i, char *name_i, char *author_i){  

    NodePtr newPtr = malloc(sizeof(Lib));

    newPtr->isbn = isbn_i;  //scanf insertion in node
    newPtr->author = author_i;
    newPtr->name = name_i;

    newPtr->nextPtr = NULL;
    NodePtr prevPtr = NULL;

    newPtr->act = 1;
    newPtr->tot = 1;

    if(lPtr == NULL){ //empty list
        *lPtr = newPtr;
        return;
    }

    NodePtr currPtr = *lPtr;

    while(strcmp(author_i, currPtr->author) < 0){ //author is different, list slides    
        prevPtr = currPtr;
        currPtr = currPtr->nextPtr;
    }

    if(strcmp(author_i, currPtr->author)== 0){ //same author

        while(strcmp(name_i, currPtr->name) < 0){ //list sliding
            prevPtr = currPtr;
            currPtr = currPtr->nextPtr;
        }

        if(strcmp(name_i, currPtr->name) == 0){ 
            currPtr->act += 1; //updates current counter and returns (to avoid duplicates)
            currPtr->tot += 1;

            free(newPtr);
            return;
        }
    }

    prevPtr->nextPtr = newPtr; //insertion between two nodes
    newPtr->nextPtr = currPtr;
}


评论