2010-08-01 69 views
4

好的,我知道還有另一個關於「exc_bad_access」的問題,但似乎與Objective-C和iPhone開發有關,而我的只是普通的C.我是C新手,用我的第一個程序完成,直到出現這個錯誤。我一直試圖弄清楚幾天,並且瘋了。任何幫助表示讚賞。 車功能:在C程序中的exc_bad_access

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

我可以在以後輸入一個字符到與fgets「編輯?」提示,但後來我得到exc_bad_access錯誤。請幫助,謝謝。代碼:

+3

請不要發送完整的代碼。我們不想全部閱讀。如果您可以縮小問題範圍並只發送相關代碼,我們可以更好地幫助您。 – zneak 2010-08-01 03:31:38

+0

更好的是,發送完整的代碼,但告訴我們哪些行是重要的。有時候看完整的代碼也可以幫助把事情搞砸。 – 2010-08-01 03:39:49

回答

6

它發生是因爲你的指針指向沒有內存。

char* z; // pointer points nowhere 
/* snip */ 
fgets(z, 2, stdin); // writing to pointer that points nowhere: boom! 

這將嘗試投入z兩個下一字符從stdin。但是,z指向無處可用。你需要它指向現有的內存:單獨聲明一個指針不足以獲得它旁邊的內存。

你可能想的2個字符的緩衝吧:

char z[2]; 

有了這個代碼,z將是一個指向足夠的內存爲2個字符。 (在C中,數組可以在任何需要指針的地方傳遞)。

我沒有看太深入你的代碼,但是對於y你會有同樣的問題。

2

您還沒有分配他們未初始化所以fgets被寫入隨機地址zy任何記憶。