c语言程序中,SIGSEGV越界访问内存的错误

大作业写的是图书管理系统,调的时候,登入新书信息,输完就闪退,单步找到了是这个错,还显示了错误的内存地址,可惜我也看不懂是哪个变量错误。。我找了几天也不知道哪里越界访问了。指针我都初始化了,用完也释放了。拜托大家帮我看一看,就快要交作业了┭┮﹏┭┮BOOKINLIB是在库图书的链表结构类型,head是头指针FBOOKINLIB是储存在文件中的结构类型登入图书的子函数代码如下/*增加新的图书*/void add_newbooks(BOOKINLIB *head) /*head只用来检查图书编码是否与之前重复*/{ FILE *fp; BOOKINLIB *p = head; long int num; char booktitle[15]; char authorname[12]; char publishhouse[20]; int inlib; double price; int kind, i = 0; if ((fp = fopen("books_info.txt", "ab")) == NULL) { printf("\n\ncannot open file or creat file.\n"); return; } system("cls"); printf("\n\n请输入希望登入的图书编号:"); scanf_s("%ld", &num); while (p != NULL) { /*检查是否已存在该编号的图书*/ if (p->book_number == num) { i++; break; } p = p->next; } if (i != 0) /*提示该编号的图书已存在*/ { printf("\n\n编号为%ld的图书已存在!该书信息如下:\n", p->book_number); printf("%-15s%-10s%-10s%-10s%-5s%-3s%-5s\n", "编号", "书名", "作者", "出版社", "价格", "在库", "借出次数"); printf("%-15d%-10s%-10s%-10s%-5.2lf%-3d%-5d\n", p->book_number, p->booktitle, p->authorname, p->publishhouse, p->price, p->inlib, p->lenttimes); printf("\n\n按任意字母键返回上一级菜单。\n"); if (getchar()) add_books(); return; } else /*添加该编号图书*/ { printf("\n请输入图书名:\t"); scanf("%s", &booktitle); printf("请输入图书数目:\t"); scanf("%ld", &inlib); printf("请输入作者:\t"); scanf("%s", &authorname); printf("请输入出版社:\t"); scanf("%s", &publishhouse); printf("请输入图书价格:\t"); scanf("%lf", &price); printf("请输入图书类型编号:\n"); printf("1.小说\n2.童书\n3.专业书\n4.工具书、手册\n5.剧本\n6.报告\n7.传记\n\n"); while (kind < 1 || kind>7) { scanf("%d", &kind); } /*在books_info文件中创建图书基本信息*/ FBOOKINLIB *pf; pf = (FBOOKINLIB *)malloc(sizeof(FBOOKINLIB)); if (pf = NULL) { printf("Memory request failed.\n"); return; } pf->book_number = num; strcpy(pf->booktitle, booktitle); strcpy(pf->authorname, authorname); strcpy(pf->publishhouse, publishhouse); pf->inlib = inlib; pf->onloan = 0; pf->lenttimes = 0; pf->kind = kind; fwrite(pf, sizeof(FBOOKINLIB), 1, fp); fflush(fp); fclose(fp); free(pf); printf("\n是否返回主菜单?(Y/N)\n"); char choice; do { choice = getchar(); } while (choice != 'Y' || choice != 'y' || choice != 'N' || choice != 'n'); if (choice == 'Y' || choice == 'y') { system("cls"); mainmenu(); } }}

if(pf=NULL)应该是if(pf==NULL)
否则你直接给他赋值NULL了,访问非法指针就会段错误。
温馨提示:答案为网友推荐,仅供参考