2013-02-26 32 views
0

所以我試圖使用順序搜索來檢查一個字符串出現在我的數組中多少次。在我的程序中,我要求用戶選擇他們希望打開並處理的文件。順序搜索和釋放數組使用C

void search(char **table, int **frequency, int wordSize) 
{ 
// Local Declaration 
int i, j; 
int count = 1; 
char target[25]; 

// Statement 
for(i = 0; i < wordSize; i++) 
{ 
    if(table[i] != NULL) 
    { 
     strcpy(target, table[i]); 
     for(j = i + 1; j < wordSize; j++) 
     { 
      if(strcmp(target, table[j]) == 0 && target != table[i]) 
      { 
       count++; 
       free(table[j]); 
       table[j] = NULL; 
      } 
     } 
    } 
    count = 1; 
} 

return; 
} 

等出了兩個文件,沒有任何問題,他們公開和過程的一個,但是當我嘗試打開它崩潰的第二個文件。我試圖理解是什麼讓我的程序崩潰,因爲這兩個文件只包含字符串,而且沒有任何字符串超過24個字符。

回答

3
if(table[j] != NULL && strcmp(target, table[j]) == 0 && target != table[i]) 

您可以訪問table變量,你必須在上一個迭代NULL版。

+0

謝謝!我沒有看到那個。我非常感謝快速回復和幫助。 – Nathan 2013-02-26 03:22:37