C程序中的exc_错误访问

C程序中的exc_错误访问,c,exc-bad-access,C,Exc Bad Access,好的,我知道这里还有另一个关于“exc_bad_access”的问题,但这似乎是针对Objective-C和iPhone开发的,而我的只是普通的C。我是C新手,在出现这个错误之前,我的第一个程序几乎已经完成了。几天来,我一直在努力想办法解决这个问题,我快发疯了。感谢您的帮助。 小车功能: void edit (int i){ char* z; char* y; char compare1[] = "on bobbin\b\b\b\b"; char compare2

好的,我知道这里还有另一个关于“exc_bad_access”的问题,但这似乎是针对Objective-C和iPhone开发的,而我的只是普通的C。我是C新手,在出现这个错误之前,我的第一个程序几乎已经完成了。几天来,我一直在努力想办法解决这个问题,我快发疯了。感谢您的帮助。 小车功能:

void edit (int i){
    char* z;
    char* y;
    char compare1[] = "on bobbin\b\b\b\b";
    char compare2[] = "not on bobbin";
    char compare3[] = "have\b\b\b\b\b\b";
    char compare4[] = "don't have";
    char wrapedit[] = "wrapped";
    char haveedit[] = "have";
    char editing[9];

    FILE *wrappedlist = fopen("../../wrapped", "r+");
    FILE *havelist = fopen("../../havelist", "r+");

    fseek(wrappedlist, i*14, SEEK_SET);
    fseek(havelist, i*11, SEEK_SET);

    printf("Edit? (y=yes, n=no)");
    fgets(z, 2, stdin);

    if ((*z=='y') && (strncmp(haveslist[i], compare4, (size_t)1) == 0)) {
        printf("Switch \"don't have\" to \"have\"? (y=yes, n=no)");
        fgets(y, 2, stdin);

        if (*y=='y') {
            fputs(compare3, havelist);
            fclose(wrappedlist);
            fclose(havelist);
            return;
        }
        else if(*y=='n'){
            fclose(wrappedlist);
            fclose(havelist);
            return;
        }
        printf("Invalid input.");
        return;
    }

    else if ((*z=='y') && (strncmp(haveslist[i], compare3, (size_t)1) == 0)) {
        fpurge(stdout);
        printf("Edit \"wrapped\" or \"have\"?");
        fpurge(stdin);
        fgets(editing, 9, stdin);

        len = strlen(editing);
        editing[len-1]='\0';

        if (strcmp(editing, wrapedit)==0) {

        if (strncmp(wrapped[i], compare1, (size_t)1)==0) {

            printf("Switch \"on bobbin\" to \"not on bobbin\"? (y=yes, n=no)");
            fgets(y, 2, stdin);
            if (*y=='y') {

                fputs(compare2, wrappedlist);
                fclose(wrappedlist);
                fclose(havelist);
                return;
            }
            else if(*y=='n'){
                fclose(wrappedlist);
                fclose(havelist);
                return;
            }
        }
            else if(strncmp(wrapped[i], compare2, (size_t)1)==0){

                fpurge(stdout);
                printf("Switch \"not on bobbin\" to \"on bobbin\"? (y=yes, n=no)");
                fgets(y, 2, stdin);
                if (*y=='y') {

                    fwrite(compare1, (size_t)strlen(compare1), 1, wrappedlist);
                    fclose(wrappedlist);
                    fclose(havelist);
                    return;
                }
                else if(*y=='n'){
                    fclose(wrappedlist);
                    fclose(havelist);
                    return;
        }
                fpurge(stdout);
                printf("Invalid input.");
        }
            fpurge(stdout);
            printf("You don't want to edit wrapped apparently.");
            fclose(wrappedlist);
            fclose(havelist);
            return;
        }
        else if(strcmp(editing, haveedit)==0){

            if (strncmp(haveslist[i], compare3, 1) == 0){

                printf("Switch \"have\" to \"don't have\"? (y=yes, n=no)");
                fgets(y, 2, stdin);
                if (*y=='y') {
                    fputs(compare4, havelist);
                    fclose(wrappedlist);
                    fclose(havelist);
                    return;
                }
                else if(*y=='n'){
                    fclose(wrappedlist);
                    fclose(havelist);
                    return;
                }
                printf("Invalid input.");
            }
            else if(strncmp(haveslist[i], compare4, 1)==0){

                printf("Switch \"don't have\" to \"have\"? (y=yes, n=no)");
                fgets(y, 2, stdin);
                if (*y=='y') {
                    fputs(compare3, havelist);
                    fclose(wrappedlist);
                    fclose(havelist);
                    return;
                }
                else if(*y=='n'){
                    fclose(wrappedlist);
                    fclose(havelist);
                    return;             
            }
        }
            printf("Invalid input.");
        }
            printf("Not editing.");

        fclose(wrappedlist);
        fclose(havelist);

        return;
    }
    else if(*z=='n'){
        fclose(wrappedlist);
        fclose(havelist);
        return;
    }

    printf("Invalid entry");
    fclose(havelist);
    fclose(wrappedlist);
    return;
}

我可以在“编辑?”提示后向FGET输入字符,但随后会出现exc\u bad\u访问错误。请帮助,谢谢。代码:

发生这种情况是因为指针指向没有内存

char* z; // pointer points nowhere
/* snip */
fgets(z, 2, stdin); // writing to pointer that points nowhere: boom!
这将尝试将
stdin
中接下来的两个字符放入
z
。但是,
z
没有任何有用的地方。您需要它指向现有内存:仅声明一个指针不足以获取它旁边的内存

您可能需要一个2个字符的缓冲区:

char z[2];
使用此代码,
z
将是指向足够存储2个字符的内存的指针。(在C语言中,可以在任何需要指针的地方传递数组。)


我没有深入研究你的代码,但是你也会遇到同样的问题,因为
y
你没有为
z
y
分配任何内存,并且
fgets
没有初始化,所以
fgets
正在写入随机地址。

请,请,请,不要发送完整的代码。我们不想全部读完。如果你能缩小问题范围,只发送相关代码,我们将更好地帮助你。更好的是,发送完整的代码,但告诉我们哪些行是重要的。有时看到完整的代码也有助于把事情弄清楚。