2016-11-01 8 views
0

次のコードでは、単語の文字ファイルを でロードしようとしていますが、ハッシュテーブル(文字列の配列)に各単語全体を保存しようとしていますが、 のように見えますが、strcpyは、単一のcharと私は理由を知らない。 strcpystrcatを乱用していますか?Cでのstrcpy、strcatの使用の競合?

# include <stdio.h> 
# include <stdlib.h> 
# include <string.h> 
# include <ctype.h> 
# include <stdbool.h> 
bool load(const char* dictionary); 

#define LENGTH 45 


int main (int argc, char* argv[]) 
{ 
    char* dictionary = argv[1]; 
    load(dictionary); 
    return 0; 
} 

bool load(const char* dictionary) 
{ 
    int index = 0, words = 0, kk = 0; 
    int lastl = 0, midl = 0; 
    char word[LENGTH + 1]; 
    char *wholeword[1001]; 

    FILE* dic = fopen(dictionary, "r"); 
    if (dic == NULL) 
    { 
    printf("Could not open %s.\n", dictionary); 
    return false; 
    } 

    for (int c = fgetc(dic); c != EOF; c = fgetc(dic)) 
    { 
    // allow only alphabetical characters and apostrophes 
    if (isalpha(c) || (c == '\'' && index > 0)) 
    { 
     // append character to word 
     word[index] = c; 
     index++; 

     // ignore alphabetical strings too long to be words 
     if (index > LENGTH) 
     { 
     // consume remainder of alphabetical string 
     while ((c = fgetc(dic)) != EOF && isalpha(c)); 
     // prepare for new word 
     index = 0; 
     } 
    } 

    // ignore words with numbers (like MS Word can) 
    else if (isdigit(c)) 
    { 
     // consume remainder of alphanumeric string 
     while ((c = fgetc(dic)) != EOF && isalnum(c)); 

     // prepare for new word 
     index = 0; 
    } 

    // we must have found a whole word 
    else if (index > 0) 
    { 
     // terminate current word 
     word[index] = '\0'; 
     lastl = index - 1; 
     midl = (index - 1) % 3; 
     words++; 
     index = 0; 

     int hashi = (word[0] + word[lastl]) * (word[midl] + 17) % 1000; 

     wholeword[hashi] = (char*) malloc(sizeof(char) * (lastl + 2)); 

     strcpy(wholeword[hashi], &word[0]); // *** 

     for (kk = 1; kk <= lastl + 1; kk++) 
     { 
     strcat(wholeword[words], &word[kk]); 
     } 
    } 
    } 
    fclose(dic); 
    return true; 
} 
+0

あなたの質問が何であるかは分かりません。 'strcpy'関数は、その名前が示すように、文字列をコピーします。 'wword'とは何ですか? –

+0

あなたはデバッガでコードをステップ実行しようとしましたか? – pm100

+0

@DavidSchwartz @DavidSchwartzは、ここでは間違っています(私はそれを編集しました)全体の単語(文字列の配列)であり、ありがとう –

答えて

2

strcpyのは、単一の文字をコピーしません、それをコピー次のヌル('\0')バイトまでのすべての文字。あなたのコードを試しに、単一の文字をコピーするには:

wholeword[hashi] = &word[0]; 

の代わり:

strcpy(wholeword[hashi], &word[0]); 
+0

配列添字の前にスペースを入れないでください。たとえば、 '&word [0]'では、優先順位は '(&word)[0]'ではなく、 '&(word [0]')です。 –

0

はい、あなたはstrcpystrcatを悪用されています。これらの関数は末尾に(先アレイへの全ソース文字列をコピーしますそこにある既存の文字列のstrcat)。

次の行:

wholeword[hashi] = (char*) malloc(sizeof(char) * (lastl + 2)); 

    strcpy(wholeword[hashi], &word[0]); // *** 

    for (kk = 1; kk <= lastl + 1; kk++) 
    { 
    strcat(wholeword[words], &word[kk]); 
    } 
} 

wholeword[hashi] = strdup(word); 

strdup()への単一の呼び出しに置き換えることができ、それにメモリ、コピー引数文字列を割り当て、ポインタを返します。

wholeword[hashi] = malloc(lastl + 2); 
    strcpy(wholeword[hashi], word); 

注:

  • あなたが衝突せずに、完璧であるためにあなたのハッシュを取るそれはあなたがそれを持っていない場合は、これらの2行を使用して、すべてのPOSIXシステム上で使用可能です。現在コード化されているように、衝突により、前の単語が辞書から削除され、対応するメモリが失われる。
  • 辞書char *wholeword[1001];は、load関数のローカル変数です。初期化されていないので、エントリが単語への有効なポインタであるかどうかを知る方法はありません。割り当てられ、NULLに初期化され、呼び出し側に返されます。
関連する問題