[英]Thread1: EXC_BAD_ACCESS (code =1, address = 0x0) in C
我面對
線程1:EXC_BAD_ACCESS(代碼= 1,地址= 0x0)
每當我嘗試掃描從輸入到char *變量的字符串時。 我不知道為什么會發生,因為一切似乎都正確。
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
頭
struct date {
int year;
int month;
int day;
};
日期結構
struct patientData {
unsigned int code;
char name[11];
char family[21];
unsigned int age;
char MF;
char disease[11];
unsigned int numOfVisits;
struct date *date1;
struct date *date2;
struct date *date3;
struct patientData *nextPtr;
};
患者數據結構
int isEmpty (struct patientData *sPtr);
void visit ( struct patientData **returned , unsigned int code);
void Insert (struct patientData **sPtr, unsigned int code , char *name , char *family , unsigned int age ,char gender, int tmgh);
void insertDisease (struct patientData **sPtr , char *name , char *family , char *disease );
struct patientData *searchCode (struct patientData **sPtr , unsigned int code, int *returnval);
struct patientData *searchName (struct patientData **sPtr , char *name , char *family);
void searchDate (struct patientData **sPtr , int year , int month , int day );
void delete (struct patientData **sPtr );
void report (struct patientData **sPtr );
職能; 這里(主要)是發生問題的地方。
int main() {
char *choice;
unsigned int code;
char name[11];
char family[21];;
char disease[11];
int searchCodeReturnValue;
unsigned int age;
char gender;
int tmgh;
int year , month , day;
struct patientData *startPtr = NULL;
puts("Enter one of the following options:");
puts("Visit");
puts("InsertDisease");
puts("search");
puts("Delete");
puts("END");
scanf("%s",choice);
while (strcmp(choice, "END") != 0) {
if (strcmp(choice, "Visit") == 0) {
printf("Enter the code:\n");
scanf("%5ui",&code);
struct patientData *a = searchCode(&startPtr,code,&searchCodeReturnValue);
if (searchCodeReturnValue == 1){
visit(&a , code);
}
else if (searchCodeReturnValue == 0){
printf("Enter name:\n");
scanf("%10s",name);
printf("Enter family:\n");
scanf("%20s",family);
printf("Enter age:\n");
scanf("%ui",&age);
printf("Enter gender:\n");
scanf("%c",&gender);
printf("Enter num of last visits:\n");
scanf("%i",&tmgh);
Insert(&startPtr , code , name , family , age , gender , tmgh);
}
}
else if ( strcmp(choice, "InsertDisease")== 0) {
printf("Enter name:\n");
scanf("%10s",name);
printf("Enter family:\n");
scanf("%20s",family);
printf("Enter disease:\n");
scanf("%10s",disease);
struct patientData *namesearch = searchName(&startPtr, name, family);
insertDisease ( &namesearch , name , family , disease );
}
else if (strcmp(choice, "Search")== 0) {
puts("Choose the way you wanna search: \n 1- by code \n 2- by first and last name \n 3- by Date");
int choiceNum;
scanf("%i",&choiceNum);
if (choiceNum == 1) {
printf("Enter the code:\n");
scanf("%5ui",&code);
searchCode(&startPtr, code , &searchCodeReturnValue);
}
else if ( choiceNum == 2){
printf("Enter name:\n");
scanf("%10s",name);
printf("Enter family:\n");
scanf("%20s",family);
searchName(&startPtr ,name , family );
}
else if ( choiceNum == 3){
printf("Enter year:\n");
scanf("%i",&year);
printf("Enter month:\n");
scanf("%i",&month);
printf("Enter day:\n");
scanf("%i",&day);
searchDate(&startPtr , year , month , day);
}
else
puts("Wrong entry");
}
else if (strcmp(choice, "delete")== 0) {
delete(&startPtr);
}
else if (strcmp(choice, "Report") == 0) {
report(&startPtr);
}
else if (strcmp(choice, "END") == 0)
return 0;
else{
puts("wrong!");
return 0;
}
}
return 0;
}
char *choice;
內存未分配給指針,您正在執行
scanf("%s",choice);
將內存分配給指針,然后嘗試掃描其值。
choice = malloc(30); /* Size can be anything of your wish */
因此,您正在訪問未初始化的指針,這將導致未定義的行為。
使用完此存儲器后,您需要釋放它
free(choice);
您正在取消引用無效的指針, choice
被聲明為char
指針並且從未初始化,您不需要將其作為char
指針,也可以將choice
聲明為char
數組,因此它所包含的最長字符串似乎是"InsertDisease"
,因為它有13個字符,所以以這種方式聲明choice
char choice[14];
並將scanf
更改為
scanf("%13s", choice);
這樣,您可以防止緩沖區溢出和內存泄漏( 如果以后沒有正確free
choice
,則可能是由於使用malloc
造成的 )。
我看到您也沒有重新掃描choice
值,這將使循環無限,您應該將其添加到循環的頂部,並在循環外將其刪除,然后將循環寫為
while (1) {
scanf("%13s", choice);
.
.
/* check the content of choice with strcmp and process the requested command */
.
.
}
在循環中,您有一個if (strcmp(choice, "END") == 0) return 0;
因此應該注意結束循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.