第24行出错,说第二个Book不允许强制类型转换

第24行出错,说第二个Book不允许强制类型转换


#include "stdio.h"    
#include "string.h"
#include "ctype.h"      
#include "stdlib.h"   

#include "math.h"  
#include "time.h"

typedef struct book {
    char name[50];
    char author[20];
    char press[30];
    char ISBN[20];
    float price;
    struct book* next;

}Book;


Book* head = NULL;
void insert(Book*p)
{
    
     Book p = (Book)malloc(sizeof(Book));
    printf("请输入书名:");
    scanf_s("%s", p->name);
    printf("请输入作者名:");
    scanf_s("%s", p->author);
    printf("请输入出版社:");
    scanf_s("%s", p->press);
    printf("请输入ISBN号:");
    scanf_s("%s", p->ISBN);
    printf("请输入价格:");
    scanf_s("%f", &p->price);
    p->next = head->next;
    head->next = p;
}


void search()
{
    char keyword[50];
    printf("请输入关键字:");
    scanf_s("%s", keyword);
    Book* p = head->next;
    while (p != NULL)
    {
        if (strcmp(p->name, keyword) == 0 || strcmp(p->author, keyword) == 0 || strcmp(p->ISBN, keyword) == 0)
        {
            printf("%s  %s  %s  %s  %.2f\n", p->name, p->author, p->press, p->ISBN, p->price);
        }
        p = p->next;
    }
}


void delete()
{
    char ISBN[20];
    printf("请输入ISBN号:");
    scanf_s("%s", ISBN);
    Book* p = head->next;
    Book* pre = head;
    while (p != NULL)
    {
        if (strcmp(p->ISBN, ISBN) == 0)
        {
            pre->next = p->next;
            free(p);
            printf("删除成功!\n");
            return  ;
        }
        pre = p;
        p = p->next;
    }
    printf("未找到对应图书信息!\n");
}


void modify()
{
    char ISBN[20];
    printf("请输入ISBN号:");
    scanf_s("%s", ISBN);
    Book* p = head->next;
    while (p != NULL)
    {
        if (strcmp(p->ISBN, ISBN) == 0)
        {
            printf("请输入书名:");
            scanf_s("%s", p->name);
            printf("请输入作者名:");
            scanf_s("%s", p->author);
            printf("请输入出版社:");
            scanf_s("%s", p->press);
            printf("请输入价格:");
            scanf_s("%f", &p->price);
            printf("修改成功!\n");
            return;
        }
        p = p->next;
    }
    printf("未找到对应图书信息!\n");
}


int main() 
{
    int p;
    head = (Book*)malloc(sizeof(Book));
    head->next = NULL;
    int choice;
    do
    {
        printf("====图书管理系统====\n");
        printf("1.插入新的图书信息\n");
        printf("2.查询图书信息\n");
        printf("3.删除图书信息\n");
        printf("4.修改图书信息\n");
        printf("5.退出系统\n");
        printf("请输入您的选择:\n");
        scanf_s("%d", &choice);
        switch (choice)
        {
        case 1:insert(p); break;
        case 2:search(); break;
        case 3:delete(); break;
        case 4:modify(); break;
        case 5:printf("欢迎再次使用!\n");
            break;
        default:printf("输入有误,请重新输入!\n"); break;
        }
    } while (choice != 5);
    return 0;
}
  • insert 方法 不需要传入参 Book *p; 主函数也不需要传
  • 修改如下:
 
#include "stdio.h"    
#include "string.h"
#include "ctype.h"      
#include "stdlib.h"   
 
#include "math.h"  
#include "time.h"
 
typedef struct book {
    char name[50];
    char author[20];
    char press[30];
    char ISBN[20];
    float price;
    struct book* next;
 
}Book;
 
 
Book* head = NULL;
void insert()
{
    
    Book *p = (Book *)malloc(sizeof(Book)); 
    printf("请输入书名:");
    scanf_s("%s", p->name);
    printf("请输入作者名:");
    scanf_s("%s", p->author);
    printf("请输入出版社:");
    scanf_s("%s", p->press);
    printf("请输入ISBN号:");
    scanf_s("%s", p->ISBN);
    printf("请输入价格:");
    scanf_s("%f", &p->price);
    p->next = head->next;
    head->next = p;
}
 
 
void search()
{
    char keyword[50];
    printf("请输入关键字:");
    scanf_s("%s", keyword);
    Book* p = head->next;
    while (p != NULL)
    {
        if (strcmp(p->name, keyword) == 0 || strcmp(p->author, keyword) == 0 || strcmp(p->ISBN, keyword) == 0)
        {
            printf("%s  %s  %s  %s  %.2f\n", p->name, p->author, p->press, p->ISBN, p->price);
        }
        p = p->next;
    }
}
 
 
void delete()
{
    char ISBN[20];
    printf("请输入ISBN号:");
    scanf_s("%s", ISBN);
    Book* p = head->next;
    Book* pre = head;
    while (p != NULL)
    {
        if (strcmp(p->ISBN, ISBN) == 0)
        {
            pre->next = p->next;
            free(p);
            printf("删除成功!\n");
            return  ;
        }
        pre = p;
        p = p->next;
    }
    printf("未找到对应图书信息!\n");
}
 
 
void modify()
{
    char ISBN[20];
    printf("请输入ISBN号:");
    scanf_s("%s", ISBN);
    Book* p = head->next;
    while (p != NULL)
    {
        if (strcmp(p->ISBN, ISBN) == 0)
        {
            printf("请输入书名:");
            scanf_s("%s", p->name);
            printf("请输入作者名:");
            scanf_s("%s", p->author);
            printf("请输入出版社:");
            scanf_s("%s", p->press);
            printf("请输入价格:");
            scanf_s("%f", &p->price);
            printf("修改成功!\n");
            return;
        }
        p = p->next;
    }
    printf("未找到对应图书信息!\n");
}
 
 
int main() 
{
    head = (Book*)malloc(sizeof(Book));
    head->next = NULL;
    int choice;
    do
    {
        printf("====图书管理系统====\n");
        printf("1.插入新的图书信息\n");
        printf("2.查询图书信息\n");
        printf("3.删除图书信息\n");
        printf("4.修改图书信息\n");
        printf("5.退出系统\n");
        printf("请输入您的选择:\n");
        scanf_s("%d", &choice);
        switch (choice)
        {
        case 1:insert(); break;
        case 2:search(); break;
        case 3:delete(); break;
        case 4:modify(); break;
        case 5:printf("欢迎再次使用!\n");
            break;
        default:printf("输入有误,请重新输入!\n"); break;
        }
    } while (choice != 5);
    return 0;
}

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^