#Solution for this error i found

4 messages · Page 1 of 1 (latest)

stark lynx
#

runtime error - accessing a field via a NULL pointer
dcc explanation:You are using a pointer which is NULL
A common error is using p->field when p == NULL.

Execution stopped in insert_book(head_shelf=0x604000000050, n=10, title=0x7ffc36bb55b0 "win_friends", author=0x7ffc36bb55f0 "dale_carnegie", genre=NON_FICTION, rating=4, pages_count=288) in cs_bookshelf.c at line 758:

        // Insert the new book after the current_book
        new_book->next = current_book->next;
        current_book->next = new_book;
    } else {
        // n is greater than the size of the list, insert at the tail
        current_book = head_shelf->books;

--> while (current_book->next != 0) {
current_book = current_book->next;
}
current_book->next = new_book;

Values when execution stopped:

current_book = NULL
head_shelf->books = NULL
new_book->next = NULL

Function Call Traceback
insert_book(head_shelf=0x604000000050, n=10, title=0x7ffc36bb55b0 "win_friends", author=0x7ffc36bb55f0 "dale_carnegie", genre=NON_FICTION, rating=4, pages_count=288) called at line 229 of cs_bookshelf.c
main()

line 758 is
while (current_book->next != 0) {

trim meadowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

night lynx
#

I'm assuming your else block is executed when the list of books is empty? But if the list of books is empty, then head_shelf->books will be NULL, so current_book will be NULL

dark willow
#

current_book = NULL
This is clearly stated in your error message