0

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

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++;
    }
2
  • 3
    Can you post code where you are filling array? Mar 2, 2014 at 5:37
  • The infinite while loop is in the scope of the for loop. Just fyi. Mar 2, 2014 at 5:41

1 Answer 1

1

Initialize pos = 0 before second while loop.

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

Edit after new problem faced by OP:

  1. Break first while loop, it is infinite now. for test, I broke it after 3 cycles.
  2. use of char* name[300] was not correct, I have corrected that.
  3. Use int row to make sure all data entered is filled in array correctly.
  4. printf() changed to %s from %c as printing done for char* and not for 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;
}
6
  • Edited, Now, I get a Seg Fault when trying to output tokenHolder[i], and when I try tokenHolder[i][i] I get the behavior described in the initial description. I tested printf("%s\n", tokenHolder[2]) to see if my values were being inputted correctly and they were. Mar 2, 2014 at 5:46
  • @user2757849 you need tokenHolder as char *tokenHolder[300]; or char tokenHolder[300];? one is Array of pointer holding each element 300 chars and other is just one array with 300 chars? Mar 2, 2014 at 6:03
  • @user2757849 Edited.. check now. Mar 2, 2014 at 6:22
  • Could explain how the functionality of the row works? Mar 3, 2014 at 23:46
  • I used a array of pointer because if I didn't the compiler gave me this warning, warning: assignment makes integer from pointer without a cast [enabled by default] token = strtok(buffer, " "); I tested using char token and char tokenHolder[200] and went I went to print the first initial position I got no output and a ton of warnings. Mar 3, 2014 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.