通过阵列C迭代时获得奇怪的输出

时间:2022-01-16 21:51:36

I have an array of char pointers as such char *arrayHolder[20]. When I try to loop through the array I get unexpected outputs if the user inputs items on the second input

我有一个char指针数组,如char * arrayHolder [20]。当我尝试循环遍历数组时,如果用户在第二个输入上输入项目,我会得到意外的输出

Example:

例:

input> 1 2 3 
1
2
3
input>2 3 4 
3
3 
3

1

Loop causing Errors:

循环导致错误:

for(i = 0; arrayHolder[i] != NULL; ++i) { 
    printf("%c\n", arrayHolder[i]); 

Code for Token input:

令牌输入代码:

char* token; 
char buffer [100];
char input[25000];
int i;
char *tokenHolder[300];
int pos = 0; 

while(1) {
    printf("repl> ");
    fgets(buffer, sizeof(buffer), stdin);
    token = strtok(buffer, " ");

    while(token != NULL) {
        tokenHolder[pos] = token;
        token = strtok(NULL, " ");
        pos++;
    }

1 个解决方案

#1


1  

Initialize pos = 0 before second while loop.

在第二次循环之前初始化pos = 0。

pos = 0; //<-- This.
while(token != NULL)
{    
   tokenHolder[pos] = token;
   token = strtok(NULL, " ");
   pos++;
}

Edit after new problem faced by OP:

在OP遇到新问题后编辑:

  1. Break first while loop, it is infinite now. for test, I broke it after 3 cycles.
  2. 先循环打破,现在是无限的。为了测试,我在3个周期后打破了它。
  3. use of char* name[300] was not correct, I have corrected that.
  4. 使用char * name [300]不正确,我已经更正了。
  5. Use int row to make sure all data entered is filled in array correctly.
  6. 使用int行确保输入的所有数据都正确填充到数组中。
  7. printf() changed to %s from %c as printing done for char* and not for char
  8. printf()从%c更改为%s作为char *的打印而不是char
int main()
{
  char* token; 
  char buffer [100];
  int i;
  char *tokenHolder[300];
  int pos = 0; 
  int row = 0;

  while(1)
  {       
      printf("repl> ");
      fgets(buffer, sizeof(buffer), stdin);
      token = strtok(buffer, " ");

      pos = 0;
      while(token != NULL){

          (tokenHolder + row)[pos] = token;
          token = strtok(NULL, " ");
          pos++;
      }
      (tokenHolder + row)[pos] = '\0';

      for(i =0; (tokenHolder + row)[i] != NULL; ++i){ 
          printf("%s\n", (tokenHolder + row)[i]); 
      }
      row++;
      if(row > 2) // Breaking of row exceeds 10. You can enter your suitable condition.
          break;
  }


  return 0;
}

#1


1  

Initialize pos = 0 before second while loop.

在第二次循环之前初始化pos = 0。

pos = 0; //<-- This.
while(token != NULL)
{    
   tokenHolder[pos] = token;
   token = strtok(NULL, " ");
   pos++;
}

Edit after new problem faced by OP:

在OP遇到新问题后编辑:

  1. Break first while loop, it is infinite now. for test, I broke it after 3 cycles.
  2. 先循环打破,现在是无限的。为了测试,我在3个周期后打破了它。
  3. use of char* name[300] was not correct, I have corrected that.
  4. 使用char * name [300]不正确,我已经更正了。
  5. Use int row to make sure all data entered is filled in array correctly.
  6. 使用int行确保输入的所有数据都正确填充到数组中。
  7. printf() changed to %s from %c as printing done for char* and not for char
  8. printf()从%c更改为%s作为char *的打印而不是char
int main()
{
  char* token; 
  char buffer [100];
  int i;
  char *tokenHolder[300];
  int pos = 0; 
  int row = 0;

  while(1)
  {       
      printf("repl> ");
      fgets(buffer, sizeof(buffer), stdin);
      token = strtok(buffer, " ");

      pos = 0;
      while(token != NULL){

          (tokenHolder + row)[pos] = token;
          token = strtok(NULL, " ");
          pos++;
      }
      (tokenHolder + row)[pos] = '\0';

      for(i =0; (tokenHolder + row)[i] != NULL; ++i){ 
          printf("%s\n", (tokenHolder + row)[i]); 
      }
      row++;
      if(row > 2) // Breaking of row exceeds 10. You can enter your suitable condition.
          break;
  }


  return 0;
}