vs2019中出现的的编译错误
#include<stdio.h>#include<stdlib.h>
struct Book
{
char title;
char author;
struct Book *next;
};//链表的创建
void getinput(struct Book *book)
{
printf("请输入书名:");
scanf_s("%s", book->title);
printf("请输入作者:");
scanf_s("%s", book->author);
}
void addbook(struct Book **head)
{
struct Book *book, *temp;
book = (struct Book*)malloc(sizeof(struct Book));//分配动态空间
if (book == NULL)
{
printf("内存分配失败!");
exit(1);
}
getinput(book);
if (*head)
{
temp = *head;
*head = book;
book->next = temp;
}
else
{
*head = book;
book->next = NULL;
}
}
void printfhead(struct Book *head)
{
struct Book *book;
int count = 1;
book = head;
while (book)
{
printf("book%d", count);
printf("书名是:%s", book->title);
printf("作者是:%s", book->author);
book = book ->next;
count++;
}
}
void releasehead(struct Book *head)
{
while (head)
{
head = head->next;
free(head);
}
}
int main(void)
{
struct Book *head=NULL;
int ch;
while (1)
{
printf("是否需要录入书籍信息(y/n):");
do
{
ch = getchar();
} while (ch != 'y' && ch != 'n');
if (ch == 'y') addbook(&head);
else break;
}
printf("是否需要打印书籍信息(y/n):");
do
{
ch = getchar();
} while (ch != 'y' && ch != 'n');
if (ch == 'y') printfhead(head);
releasehead(head);
return 0;
}
错误:
0x7BA3EF8C (ucrtbased.dll)处(位于 lianxi.exe 中)引发的异常: 0xC0000005: 写入位置 0x00E02000 时发生访问冲突。
在viscual c++ 6.0编译没有问题,求解决方法
void releasehead(struct Book *head)
{
while (head)
{
head = head->next;
free(head);
}
}
这里逻辑不对
应该是保存当前,然后让head指向下个节点,然后释放刚刚保存的当前的指针
你这个先指向下个节点,然后释放下个节点了 当前节点根本就没做任何操作 yuxijian2020 发表于 2021-8-18 11:54
这里逻辑不对
应该是保存当前,然后让head指向下个节点,然后释放刚刚保存的当前的指针
你这个先 ...
void releasehead(struct Book *head)
{
while (head)
{
Book* ptmp=head->next;
free(head);
head=ptmp;
}
}
页:
[1]