简体   繁体   中英

Char* showing null when it shouldn't

In this function I wrote for a book database program, I get the user to input a book identifier and it should then go and search the linkedlist for that book and display it's details.

The identifierTemp variable is initialized as NULL but when the user inputs their desired book's identifier, the identifierTemp remains as NULL ?!?!

Here's the function. Hopefully someone can spot my mistake. Thanks in advance.

void viewBook() {

    printf("\n*** VIEW A BOOK ***\n");

    struct node *currentBook, *prevBook;

    bool notFound = true;

    char* identifierTemp = NULL;

    //user input
    printf("\nBook Identifier: ");
    scanf("%s", identifierTemp);
    fflush(stdin);

    printf("\n");

    if (isEmpty())
        printf("Error - there are no books in the list\n\n\n");
    else  {
        currentBook = prevBook = firstBook;

        while (notFound && currentBook != NULL) {

            if (identifierTemp == currentBook->element->identifier)
                notFound = false;
            else {
                    prevBook = currentBook;
                    currentBook = currentBook->next;
            }//end else

        } //end while

        if (notFound)
            printf("Error - there is not such book with the identifier %s\n\n\n", identifierTemp);

        else  {

            //DISPLAY ALL BOOK DETAILS
            printf("\n\nIdentifier: %s", currentBook->element->identifier);
            printf("\nTitle: %s", currentBook->element->title);
            printf("\nAuthor: %s", currentBook->element->author);
            printf("\nYear: %d", currentBook->element->year);
            printf("\nStatus (1 = not available to take out): %d", currentBook->element->status);
            printf("\nCustomer: %s", currentBook->element->customer);
            printf("\nTimes Taken Out: %d", currentBook->element->timesTakenOut);
            printf("\nGenre: %s", currentBook->element->genre);
            } //end else

            printf("\n");

       }//end else

    menuSystem();//return to the menu

}//end ViewBook

Console output:

*** VIEW A BOOK ***

Book Identifier: *USER INPUT HERE*

Error - there is no such book with the identifier (null)

You need to give identifierTemp some memory so scanf has somewhere to put the data

ie change it to

char identifierTemp[4096]; // Or some other size that can reasonably store the string.

The re-read the manual page on scanf so that limit will not be exceeded

After you allocate memory properly for your identifierTemp you also need to compare strings properly. This code:

identifierTemp == currentBook->element->identifier

Compares the string addresses (pointers) and will not ever be true. You need to use strcmp :

if (strcmp(identifierTemp, currentBook->element->identifier) == 0)
    notFound = false;

By the way, the double negative is a bit distasteful. Why not make the boolean be Found = false to start and then set Found = true when you find it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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