以下は、私が作成した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()を実装するのですか?私はそれについて読んできて、うまくいくように思えますが、私はそれらの機能を実装することができません。
は '* newToken'は未定義の動作の原因となる。また –
あなたの関数(あなたはnewToken''のうちの最初の文字を読んでいますが、内容を初期化することはありません) 'char **'を返すと宣言されていますが、 'return'ステートメントはありません –
私はreturnステートメントをまだ実装していません。 あなたが言っていることは、配列を作るときに配列の内容を初期化する必要があるということです。だから、私は配列をループし、すべてのインデックスにランダムな値を入れます。 –