2017-02-23 1 views
0

以下は、私が作成したTokenizerのセクションです。ユーザーはトークン化する文字列を入力し、その文字列はchar配列に格納され、文字列が終了するとすぐにヌル文字( '\ 0')が配置されます。コードのその部分は、それを数回テストした後に正常に動作するようです。ループがC言語のヌル文字に達する前に終了するようです。

私は配列(tokenArray)アレイ(newToken)を作るとき、私は取得しています問題は、コードの後半に起こります。私は、トークンの数とトークンの長さを得る関数を使用します。

文字列「試験鉛筆電卓」を入力しました。次に、各トークンを配列に格納します。問題は、配列の内容を印刷するときに、印刷が必要な前に停止しているループがあることです。

ここにサンプルの入出力があります。 (ないコードで)私のコメントは私の人生のため

$testing pencil calculator //string entered 

complete index: 0  //index of the entire array, not the tokenized array 
token length: 7   //length of 1st token "testing" 
pointer: 0xbf953860 
tokenIndex: 0   //index of the token array (array of arrays) 
while loop iterations: 4 //number of times the while loop where i print is iterated. should be 7 
test      //the results of printing the first token 

complete index: 8             
token length: 6   //next token is "pencil"                    
tokenIndex: 1              
while loop iterations: 5 //should be 6          
penci     //stops printing at penci  

complete index: 15             
token length: 10   //final token is "calculator"           
pointer: 0xbf953862             
tokenIndex: 2               
while loop iterations: 5 //should be 10            
calcu     //stops printing at calcu 

によって指摘whileループが終了している理由は、それがすることになっている前に、私は単純に把握することはできません。これが私の方法論の唯一の問題だとは思えませんが、これを理解するまでは他のバグに対処できません。メインから

::私はエラーを取得しています

completeString[inputsize] = '\0'; 

    char tokenArray[numTokens+1]; 
    tokenArray[numTokens] = '\0';  
    putTokensInArray(tokenArray, completeString); 

方法:私はいくつか試してみました

char ** putTokensInArray(char tokenArray[], char * completeString){ 
    int completeIndex = 0; 
    int tokenIndex = 0; 

    while(tokenArray[tokenIndex] != '\0'){ 
    int tokenLength = tokenSize(completeString, completeIndex); 
    char newToken [tokenLength+1]; 
    newToken[tokenLength] = '\0'; 
    tokenArray[tokenIndex] = *newToken; 

    printf("\ncomplete index: %d", completeIndex); 
    printf("\ntoken length: %d", tokenLength); 
    printf("\ntokenIndex: %d\n", tokenIndex); 

    int i = 0; 
    while(newToken[i] != '\0'){ 
     newToken[i] = completeString[i + completeIndex]; 
     i++; 
    } 
    completeIndex += (tokenLength+1); 

    printf("while loop iterations: %d\n", i); 

    for(int j = 0; newToken[j] != '\0'; j++){ 
     printf("%c", newToken[j]); 
    } 

    tokenIndex++; 
    tokenLength = 0; 

    }//big while loop 
}//putTokensInArray Method 

以下

は、このために責任がある私のコードのセクションですものだけではそれの把握を得ることはできません。私はC言語に新しいので、私はポインタミスをしたり、メモリにアクセスしたりしてはいけません。そのメモで、私はどのようにmalloc()とfree()を実装するのですか?私はそれについて読んできて、うまくいくように思えますが、私はそれらの機能を実装することができません。

+0

は '* newToken'は未定義の動作の原因となる。また –

+0

あなたの関数(あなたはnewToken''のうちの最初の文字を読んでいますが、内容を初期化することはありません) 'char **'を返すと宣言されていますが、 'return'ステートメントはありません –

+0

私はreturnステートメントをまだ実装していません。 あなたが言っていることは、配列を作るときに配列の内容を初期化する必要があるということです。だから、私は配列をループし、すべてのインデックスにランダムな値を入れます。 –

答えて

0

初期化されていない文字配列を関数putTokensInArrayに渡しています。その機能の後半では、whileループ状態で、0から始まるすべての要素に対して\0をチェックしています。ただし、配列は初期化されていないため、これらの文字はすべての文字になります。 numTokens+1要素の前に\0が存在する可能性があります。

この問題を解決するには、文字配列の長さ、つまりnumTokensを追加引数としてputTokensInArrayに渡します。次に、あなたのwhileループでは、代わりに以下の条件チェックを実行します。

while(tokenIndex < numTokens){

+0

ありがとうございます。これはダムと聞こえるかもしれませんが、Cで配列の内容を初期化するための正しい方法や標準的な方法はありますか? -1をすべてのインデックスなどに配置しますか? これは、mallocと無料で遊ぶ場所ですか?私は初心者ですので、私はまだこれらのこと全てを理解しようとしています。私はあなたの助けに感謝します! –

+0

@OmarKhalik、char配列を初期化するのは簡単です。 'char tokenArray [numTokens + 1] =" ";'これは、すべての要素を0に初期化します。 – VHS

関連する問題