다음은 내가 작성한 Tokenizer의 섹션입니다. 사용자는 토큰화할 문자열을 입력하고 그 문자열은 char 배열에 저장되며 문자열이 끝나는 즉시 null 문자 ('\ 0')가 배치됩니다. 코드의 해당 부분은 몇 번 테스트 한 후에 잘 작동하는 것 같습니다. 내가 배열 (tokenArray) 배열 (newToken)의을 할 때루프가 C에서 null 문자에 도달하기 전에 종료 된 것으로 보입니다
는내가 갖는 문제는 나중에 코드에서 발생합니다. 함수를 사용하여 토큰 수와 토큰 길이를 구합니다.
"연필 계산기 테스트 중"이라는 문자열을 입력했습니다. 그런 다음 각 토큰을 배열에 저장합니다. 문제는 배열의 내용을 인쇄 할 때 인쇄를해야하는 루프가 중지되기 전에 멈추는 것입니다.
다음은 샘플 입출력입니다. 나의 코멘트 (안 코드에서) 내 인생
$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 문을 아직 구현하지 않았습니다. 배열을 만들 때 배열의 내용을 초기화해야한다는 것입니다. 그래서 배열을 반복하고 모든 인덱스에 무작위 값을 넣을까요? –