简体   繁体   English

线程1:C中的EXC_BAD_ACCESS(代码= 1,地址= 0x0)

[英]Thread1: EXC_BAD_ACCESS (code =1, address = 0x0) in C

I face with 我面对

Thread1: EXC_BAD_ACCESS (code =1, address = 0x0) 线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)

whenever I try to scan a string from input to a char * variable. 每当我尝试扫描从输入到char *变量的字符串时。 I've got no idea why it occurs , because everything seems right. 我不知道为什么会发生,因为一切似乎都正确。

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

headers

struct date {
    int year;
    int month;
    int day;
};

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

};

Patient data structure 患者数据结构

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

functions; 职能; here (main) is where the problem occurs. 这里(主要)是发生问题的地方。

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;

Memory is not allocated to the pointer and you are doing 内存未分配给指针,您正在执行

scanf("%s",choice);

Allocate memory to the pointer and later try to scan value to it. 将内存分配给指针,然后尝试扫描其值。

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

So you are accessing uninitialized pointer which will lead to undefined behavior. 因此,您正在访问未初始化的指针,这将导致未定义的行为。

Once done with using this memory you need to free it 使用完此存储器后,您需要释放它

free(choice);

You are dereferencing an invalid pointer, choice is declared as a char pointer and never initialized, you don't need it to be a char pointer, can declare choice as a char array too, the longest string it would contain seems to be "InsertDisease" which has 13 characters, so declare choice this way 您正在取消引用无效的指针, choice被声明为char指针并且从未初始化,您不需要将其作为char指针,也可以将choice声明为char数组,因此它所包含的最长字符串似乎是"InsertDisease" ,因为它有13个字符,所以以这种方式声明choice

char choice[14];

and change scanf to 并将scanf更改为

scanf("%13s", choice);

that way you prevent a buffer overflow, and a memory leak too ( which would be caused by using malloc if you don't properly free choice later ). 这样,您可以防止缓冲区溢出和内存泄漏( 如果以后没有正确free choice ,则可能是由于使用malloc造成的 )。

I see that you also don't rescan for choice value, which will make your loop infinite, you should add this to the top of the loop, and remove it outside the loop, then write the loop as 我看到您也没有重新扫描choice值,这将使循环无限,您应该将其添加到循环的顶部,并在循环外将其删除,然后将循环写为

while (1) {
    scanf("%13s", choice);
    .
    .
   /* check the content of choice with strcmp and process the requested command */
    .
    .
}

in the loop you have a if (strcmp(choice, "END") == 0) return 0; 在循环中,您有一个if (strcmp(choice, "END") == 0) return 0; so that should take care of ending the loop. 因此应该注意结束循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM