丶幽惑 丶幽惑
关注数: 14 粉丝数: 8 发帖数: 806 关注贴吧数: 69
萌新求问调用结构体指针作为参数 什么时候用**p 什么时候用*p ? void addBook(struct Book *library) 这里的参数 我想使用library的指针, 但是 打印输出图书的时候是没有任何输出的 只能是void addBook(struct Book **library) 才可以输出正常结果 ,这是为什么呢... 求教各位大佬 #include <stdio.h> #include <stdlib.h> struct Book { char title[128]; char author[40]; struct Book *next; }; void inputInformation(struct Book *); void addBook(struct Book *); void printLibrary(struct Book *); void releaseLibrary(struct Book *); void inputInformation(struct Book *book) { printf("Title:"); scanf("%s",book->title); printf("author:"); scanf("%s",book->author); } void addBook(struct Book *library) { struct Book *book, *temp; book = (struct Book *)malloc(sizeof(struct Book)); inputInformation(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("Name:%s\n",book->title); book = book->next; count++; } } void releaseLibrary(struct Book *library) { struct Book *temp; while (library != NULL) { temp = library; library = library->next; // Error free(temp); } } int main() { struct Book *library; int ch; while (1) { printf("是否录入书籍信息"); do { ch = getchar(); } while (ch != 'Y' && ch !='N'); if (ch == 'Y') { addBook(library); } else { break; } } printf("是否需要打印图书信息?"); do {ch = getchar();} while (ch != 'Y' && ch != 'N'); if (ch == 'Y') {printLibrary(library);} releaseLibrary(library); return 0; }
1 下一页