gpt4 book ai didi

c - 错误: expected expression before 'book'

转载 作者:行者123 更新时间:2023-11-30 19:32:19 24 4
gpt4 key购买 nike

我是一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com