一轮江月明 发表于 2017-4-13 08:38:30

带你学C带你飞S1E45有疑问

#include <stdio.h>
#include <stdlib.h>

struct Book
{
        char title;
        char author;
        struct Book *next;
};

void getInput(struct Book *book);
void addBook(struct Book **library);
void printLibrary(struct Book *library);
void releaseLibrary(struct Book *library);

void getInput(struct Book *book)
{
        printf("请输入书名:");
        scanf("%s", book->title);
        printf("请输入作者:");
        scanf("%s", book->author);
}

void addBook(struct Book **library)
{
        struct Book *book, *temp;

        book = (struct Book *)malloc(sizeof(struct Book));
        if (book == NULL)
        {
                printf("内存分配失败了!\n");
                exit(1);
        }

        getInput(book);

        if (*library != NULL)
        {
                temp = *library;
                *library = book;
                book->next = temp;       
        }
        else
        {
                *library = book;
                book->next = NULL;
        }
}

void printLibrary(struct Book *library)
{
        struct Book *book;
        int count = 1;

        book = library;
        while (book != NULL)
        {
                printf("Book%d: \n", count);
                printf("书名: %s\n", book->title);
                printf("作者: %s\n\n", book->author);
                book = book->next;
                count++;
        }
}

void releaseLibrary(struct Book *library)
{
        while (library != NULL)
        {
                free(library);
                library = library->next;
        }
}

int main(void)
{
        struct Book *library = NULL;
        int ch;

        while (1)
        {
                printf("请问是否需要录入书籍信息(Y/N):");
                do
                {
                        ch = getchar();
                } while (ch != 'Y' && ch != 'N');

                if (ch == 'Y')
                {
                        addBook(&library);//这里和addBook(**library)是一样的么?
                }
                else
                {
                        break;
                }
        }

        printf("\n\n请问是否需要打印图书信息(Y/N):");
        do
        {
                ch = getchar();
        } while (ch != 'Y' && ch != 'N');

        if (ch == 'Y')
        {
                printLibrary(library);
        }

        releaseLibrary(library);

        return 0;
}


第24行和第90行为什么不一样呢?第90行要是写成addBook(**library)会报错,(**library)和(&library)不都是取指针的地址的意思么?

ljd884497 发表于 2017-4-13 09:55:00

(**library)和(&library) 我觉得这两个是不一样的,首先&library 是取指针变量library 地址是对的,但
**library 不是取指针变量library地址,library 首先是一个指针变量,*library就是取library所指向内存里的值,在此程序中library = NULL,就是空指针,没有指向那个具体的值,此时再去用‘*’取 一个空值是没有意义的,就像这样*(*library])= * (NULL),这样是没有意义的.

qq1242009750 发表于 2017-4-15 10:24:09

参数为 *library 我们要传入的 是一个变量的地址 , 如果参数为 **library 那么我们就要传入一个指针的地址

*library是一个指针 ,**library是一个指向指针的指针{:10_256:}

一轮江月明 发表于 2017-4-15 14:44:54

qq1242009750 发表于 2017-4-15 10:24
参数为 *library 我们要传入的 是一个变量的地址 , 如果参数为 **library 那么我们就要传入一个指针的地址 ...

终于明白了,感谢您的回复!
页: [1]
查看完整版本: 带你学C带你飞S1E45有疑问