- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一个 stackoverflow 浏览器,我正在学习第一门 C 类(class)。我已经为学校作业编写了一些代码大约一天了。我应该将一个节点插入到链表的中间。截至目前,我的大部分代码正在编译。我在使用我声明的函数之一时遇到错误:
bookshelves.c: In function ‘main’:
bookshelves.c:53:14: error: expected expression before ‘book’
print_shelf(book *head);
^~~~
bookshelves.c:62:14: error: expected expression before ‘book’
print_shelf(book *head);
^~~~
bookshelves.c:66:14: error: expected expression before ‘book’
print_shelf(book *head);
^~~~
我在代码中使用了3次声明的函数,但是函数的声明没有错误。我在网上找到的任何内容都不足以帮助我解决问题。我想我可能在函数声明或其他东西中使用了错误的变量,但改变它只会让我的代码更加困惑。
这是我的完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct book_struct {
char *titles;
struct book_struct *next;
} book;
void print_node(book *next);
void print_shelf(book *head);
int main() {
book *A = malloc(sizeof(book));
book *B = malloc(sizeof(book));
book *C = malloc(sizeof(book));
book *D = malloc(sizeof(book));
book *E = malloc(sizeof(book));
A->titles = malloc(20);
B->titles = malloc(20);
C->titles = malloc(20);
D->titles = malloc(20);
E->titles = malloc(20);
strcpy(A->titles, "War and Peace");
A->next = NULL;
strcpy(B->titles, "War on Drugs");
B->next = NULL;
strcpy(C->titles, "War ");
C->next = NULL;
strcpy(D->titles, "War Horse");
D->next = NULL;
strcpy(E->titles, "War of 1812");
E->next = NULL;
book *shelf = A;
A->next = B;
B->next = C;
C->next = D;
D->next = E;
E->next = NULL; // always remember to set "next" of the last book to NULL
printf("Bookshelf before inserting new book:\n");
print_shelf(book *head);
{
book new_book;
strcpy(new_book.titles, "Twilight");
new_book.next = NULL;
C->next = &new_book;
new_book.next = C;
print_shelf(book *head);
}
printf("Bookshelf after inserting new book:\n");
print_shelf(book *head);
return 0;
}
void print_node(book *next) {
printf("%c\n", *next->titles);
}
void print_shelf(book *head) {
book *current = head;
while (current != NULL) {
print_node(current);
//Advance current
current = current->next;
}
}
我尝试了不同的方法来修复该错误,但没有任何效果。希望您能提供有关该错误的帮助,您不必修复整个代码。一旦我克服了这个错误,我就可以从那里开始。
感谢您的帮助!
最佳答案
因此,您最初的错误是调用语法不正确,可以通过将 print_shelf(book *head);
更改为 print_shelf(shelf)
来纠正。我还重写了您的代码,以说明链表功能强大,并且不需要元素本身之外的每个元素的单独指针(我已经排除了释放
元素,因为程序会无论如何立即终止):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct book_struct {
char *titles;
struct book_struct *next;
} book;
void add_node(book* head, book* newNode);
void print_node(book *next);
void print_shelf(book *head);
char* titles[] = {"War and Peace", "War on Drugs", "War ", "War Horse", "War of 1812" };
int main() {
book* head = malloc(sizeof(book));
{
book* currNode = head;
int i = 0;
for(; i < 4; ++i)
{
currNode->titles = malloc(20);
strcpy(currNode->titles, titles[i]); // malloc is technically not neccessary here, just for consistancy with your original code :)
currNode->next = malloc(sizeof(book));
currNode = currNode->next;
}
currNode->titles = malloc(20);
strcpy(currNode->titles, titles[i]);
currNode->next = NULL;
}
printf("Bookshelf before inserting new book:\n");
print_shelf(head);
{
book* new_book = malloc(sizeof(book));
new_book->titles = malloc(20);
strcpy(new_book->titles, "Twilight");
new_book->next = NULL;
add_node(head, new_book);
}
printf("Bookshelf after inserting new book:\n");
print_shelf(head);
return 0;
}
void print_node(book *next) {
printf("%s\n", next->titles);
}
void print_shelf(book *head) {
book *current = head;
while (current != NULL) {
print_node(current);
//Advance current
current = current->next;
}
}
void add_node(book* head, book* newNode) {
book* current = head;
while(current->next) current = current->next;
current->next = newNode;
}
关于c - 错误: expected expression before 'book' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47243007/
我正在为 MIT OCW 类(class)做一些事情,它要求编写一个“图书馆”类(class)。现在我有这个: #include using namespace std; class Book{
ExcelWorkbook = py.load_workbook(FilePath) writer = pd.ExcelWriter(FilePath, engine = 'openpyxl') wr
我是 C++ 的学习者,我对构造函数和析构函数感兴趣。我编译了下面的代码,它返回了对 Book::~Book() 错误的 undefined reference 。但是当我注释掉析构函数时,它工作正常
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在使用 Google Books API在我的 Angular 项目中。我有书籍的不同静态类别的列表。单击特定类别时,我想从 Google Books API 获取搜索类别的书籍。 Google
我不知道如何做一些应该非常简单的事情。 我有两个实体:书架和书。一个书架可以放一本或多本书。这些实体中的每一个都有一个相应的 JpaRepository 暴露为使用 Spring Data Rest
我需要从 Booking.com 系统获取特定住宿的评分值。是否有 API 可以提供总体评分等信息以及系统中列出的属性的其他有用信息? 最佳答案 如果有人有同样的问题,我会简短地回答 - bookin
我需要从 Booking.com 系统获取特定住宿的评分值。是否有 API 可以提供总体评分等信息以及系统中列出的属性的其他有用信息? 最佳答案 如果有人有同样的问题,我会简短地回答 - bookin
有没有一种简单的方法可以使用 Google Book API 从 ISBN 获取 JSON 格式的图书封面? 最佳答案 您可以使用 isbn: 查询,如下所示: https://www.googlea
我是新手,所以这只是一个问题,我想知道哪个更有效,哪个提供最佳时间复杂度。 没有。 1 export default class BookingTabs extends Component {
在我一直在开发的应用程序中,我遇到了像 /books/:slug, :to => 'books#show', slug:/.*?/ 这样的路由。我很好奇这部分的作用 slug:/.*?/ ? 最佳答案
刚从使用 Books 应用程序示例的 Djangobook 教程中学习时,您通过多对多关系将 Book 与 Author 相关,并将 Book 与 Publisher 相关。您可以使用 p.book_
我刚启动 xcode 7 稳定版。在我当前的项目中,我正在从 web 服务下载图像。在 xcode 6.4 中工作正常。现在它没有显示任何图像并在日志中显示警告 -canOpenURL: failed
我在名为 DetailOrder 和 Book 的两个类中遇到映射问题。 问题如下所示。 Initial SessionFactory creation failed. org.hibernate.A
我正在尝试制作 CRUD+spring 应用程序来创建/删除/更新书籍。一切正常,但从数据库中搜索一本书。请帮忙。 @Controller public class BookController {
我完全是使用 CakePhp 的新手。我已经解决了一些问题,但我又对这个基本问题了如指掌。你能帮我解决这个问题吗? Notice (8): Undefined variable: books [APP
我在网上冲浪时遇到了一对多关系的问题,并且无法修复。我仔细检查了他们引用的 @Entity 声明 import javax.persistence.Entity; 之前我仅尝试单向 ManyToOne
刚开始在我的学校学习 node js。他们给了我们这个半完成的任务,我需要让下一个和上一个按钮起作用。但是,当我运行 index.html 时,控制台出现了一些错误。错误是: “获取 API 无法加载
我创建了一个网站,访问者可以使用turnjs 浏览一本书。我的页面是双页的 jpg,我希望它们能够动态加载。 代码如下: var flipbook = $('.flipbook'); flipboo
void displayInventory(const struct Book book[], const int size) { Idk y book[] 在 visual studio 中遇到
我是一名优秀的程序员,十分优秀!