2016-03-14 57 views
0

時所以基本上,當我編譯我的代碼以GCC編譯器我沒有得到任何錯誤或警告,但錯誤,當我輸入數據的第一塊,它說:總線錯誤:10,無編制

Bus error: 10. 

我我不知道我做錯了什麼。我認爲問題來自void anagramGrouping(最後一個功能)。我還包含了其他代碼以幫助遵循邏輯。

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

#define Row 2 
#define col 20 

int wordCount = 0; 
int groupCount = 0; 
char wordList[Row][col]; 
char group[Row][col]; 

// this is where prototypes go 
void sortword(char word[col]);     
void anagramGrouping(char word[col], char copy[col]); 
void resetGroup();         

int main() { 
    int i; // used in for loop to 'get' the strings 
    char word[col]; 

    resetGroup(); 

    for (i = 0; i < Row; i++) { 
     scanf("%s", word); 
     sortword(word); 
     wordCount++; 
    } 
} 

void resetGroup() { 
    int i; 

    for (i = 0; i < Row; i++) 
     strcpy(group[i], " "); 
} 

void sortword(char word[col]) { 
    int i = 0; 
    char temp; 
    char copy[col]; // used to store a copy of the original word 

    strcpy(copy, word); 

    while (word[i] != '\0') { 
     int j = i + 1; 

     while (word[j] != '\0') { 
      if (word[j] < word[i]) { 
       temp = word[i]; 
       word[i] = word[j]; 
       word[j] = temp; 
      } 
      j++; 
     } 
     i++; 
    } 
    anagramGrouping(word,copy); 
} 

void anagramGrouping(char word[col], char copy[col]) { 
    int n; 

    if (wordCount == 0) { 
     strcpy(group[0], copy); 
    } 

    for (n = 0; n <= groupCount; n++) { 
     if (strcmp(group[n], word) == 0) { 
      strcpy(group[n], copy); 
     } else { 
      groupCount++; 
      strcpy(group[groupCount], copy); 
     } 
    } 
} 
+1

在valgrind或gdb中運行你的程序來查看哪一行觸發崩潰。 – immibis

+0

對不起,沒有更多的幫助,但嘗試檢查你的指針/取消引用。 –

+0

您的代碼非常糟糕。修復縮進並刪除無用的'//結束函數'註釋。 – chqrlie

回答

-1

對於初學者來說,更改所有

sortword (char word[col]) 

sortword (char *word) 

和所有

anagramGrouping (char word[col], char copy[col]) 

anagramGrouping (char *word, char *copy) 

當您通過char copy[col],要傳遞一個character array,其通過當作爲函數參數被轉換爲指針。 (你會聽到短語「衰減到指針」,這是不完全正確的,但通常用於表示同樣的事情)。這不是導致bus error的問題,但會使您的代碼更具可讀性。

接着,您的bus error一般是由於其被獲取的值時的指針值的誤用,落在容許範圍存儲器爲程序以外,通常在系統區域造成bus error。仔細查看你的代碼,很難分辨出哪些問題可能是確切的原因。然而,一個可能的罪魁禍首是:

for(n=0; n <= groupCount; n++) 
{ 
    if (strcmp(group[n],word) ==0) 
    { 
     strcpy(group[n],copy); 
    } 
    else 
    { 
     groupCount++; 
     strcpy(group[groupCount],copy); 
    } 
} 

凡通過調試運行,你會很快發現,groupCount增長遠遠超出1造成strcpy(group[groupCount],copy)寫超越char group[Row][col];結束。如果允許的行值限制爲0-1#define Row 2一樣。

要限制拷貝到允許的範圍內,改變循環標準:

for (n = 0; n < Row; n++) { 

您的代碼可能看起來爲進一步清理如下:

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

#define Row 2 
#define col 20 

int wordCount = 0; 
int groupCount = 0; 
char wordList[Row][col]; 
char group[Row][col]; 

void sortword (char *word);     
void anagramGrouping (char *word, char *copy); 
void resetGroup();         

int main (void) { 

    int i; 
    char word[col] = ""; 

    resetGroup(); 

    for (i = 0; i < Row; i++) 
    { 
     scanf ("%s", word); 
     sortword (word); 
     wordCount++; 
    } 

    return 0; /* main is a function of type 'int' and returns a value */ 
} 

void resetGroup() 
{ 
    int i; 

    for (i = 0; i < Row; i++) 
     *group[i] = 0; 
} 

void sortword (char *word) 
{ 
    int i = 0; 
    char temp; 
    char copy[col] = ""; 

    strcpy (copy, word); 

    while (word[i]) { 
     int j = i + 1; 
     while (word[j]) { 
      if(word[j] < word[i]) { 
       temp = word[i]; 
       word[i] = word[j]; 
       word[j] = temp; 
      } 
      j++; 
     } 
     i++; 
    } 
    anagramGrouping (word,copy); 
} 

void anagramGrouping (char *word, char *copy) 
{ 
    int n; 

    if (!wordCount) 
     strcpy (group[0],copy); 

    for (n = 0; n < Row; n++) { 
     if (strcmp (group[n], word) == 0) 
      strcpy(group[n],copy); 
     else { 
      groupCount++; 
      strcpy (group [groupCount],copy); 
     } 
    } 
} 

讓我知道你是否有任何問題。

+0

關於不傳遞數組的全部胡說與這個問題以及糟糕的建議無關。 'char word [col]'比'char * word'更可讀,更安全。如果有的話,函數應該改爲'sortword(size_t col,char word [col])',使它獨立於宏,甚至更安全。 – Lundin

+0

另外關於你的關於衰減的評論是不正確的術語...轉換更不正確,因爲標準使用的術語轉換是在運行時發生的。陣列衰變不。 C標準實際上使用「調整」這個詞來描述這一點(6.7.6.3):「將一個參數聲明爲'''數組'的類型''調整爲''合格指針' type'',其中類型限定符(如果有)是在數組類型派生的[和]中指定的那些限定符。「 – Lundin

+0

好點。 ''調整'它是 - 它完成後留下一個數組轉換爲指針':)' –