简体   繁体   中英

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

I face with

Thread1: EXC_BAD_ACCESS (code =1, address = 0x0)

whenever I try to scan a string from input to a char * variable. 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

char choice[14];

and change scanf to

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

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

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; so that should take care of ending the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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