0

下面是我构建的 Tokenizer 的一部分。用户键入一个他们希望标记的字符串,该字符串存储到一个 char 数组中,一旦字符串结束,就会放置一个空字符 ('\0')。这部分代码在经过几次测试后似乎运行良好。

当我创建数组(newToken)的数组(tokenArray)时,我遇到的问题稍后会出现在代码中。我使用函数来获取令牌数量和令牌长度。

我输入了字符串“测试铅笔计算器”。然后我将每个令牌存储到一个数组中。问题是当我去打印数组的内容时,我打印的循环在它应该之前停止了。

这是一个示例输入/输出。我的评论(不在代码中)由

$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()?我一直在阅读它并且似乎可以工作,但我无法实现这些功能。

4

1 回答 1

0

您正在将未初始化的字符数组传递给您的函数putTokensInArray。稍后在该函数中,在while循环条件中,您将检查\0从 0 开始的每个元素。但是,由于数组未初始化,因此这些字符可以是任何字符。在第 th 元素\0之前可能有一个。numTokens+1

要解决此问题,请将字符数组的长度 ie作为附加参数传递numTokens给。putTokensInArray然后在您的 while 循环中,改为执行以下条件检查:

while(tokenIndex < numTokens){

于 2017-02-23T04:18:35.563 回答