2015-01-13 48 views
0

我面對(碼= 1,地址=爲0x0)線程1:EXC_BAD_ACCESS用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; 
}; 

日期sructure

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; 
} 

回答

0

您正在訪問一個空指針,choice聲明爲char指針從不初始化,你不需要它是一個char指針,可以聲明choice作爲char陣列也將包含最長的字符串似乎是"InsertDisease"其中有13個字符,所以聲明choice這樣

char choice[14]; 

,改變scanf

scanf("%13s", choice); 

這樣可以防止緩衝區溢出和內存泄漏太(這將通過使用malloc如果你沒有正確freechoice後來引起的)。

我看你也不要重新掃描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;,所以應該照顧結束循環。

0
char *choice; 

內存不是分配給指針和你正在做

scanf("%s",choice); 

分配內存的指針,後來嘗試掃描值到它。

choice = malloc(30); /* Size can be anything of your wish */ 

所以你正在訪問未初始化的指針,這將導致未定義的行爲。

一旦使用這種內存做了你需要釋放它

free(choice); 
+0

真的嗎?但我之前使用過這種風格,而且我從來沒有收到過這樣的錯誤,爲什麼它需要內存分配? '#include int main(){ char * mine; scanf(「%s」,mine); 012fprintf(「%s \ n」,mine); return 0; }' – user21087

+0

@ user21087是的,爲了將您的值存儲到某個內存位置,您需要爲其分配內存。您可以使用'malloc()'完成相同的操作,如圖所示 – Gopi

相關問題