Thread: strtok

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    61

    strtok

    hi again, i have the following code and it has some problem. after doing strtok for the 1st time, when i print out the value of line[0], it returns "hello -a" which is correct. then after the next strtok in the for loop, the result by printing line[0] changes to "hello" i did not do anything to change the value.. so how did it got modified? isit because i did temp = strtok(line[i], ""); and line[i] got changed in the process? please help thanks!
    Code:
    char* string;
    string = "hello -a | world -b";
    char** line;
    char** line2;
    char* temp;
    int i, count;
    i = count = 0;
    //do malloc for line and line 2...
    ......
    .....
    
    /*count number of separated commands */
    for(i = 0; i < strlen(string); i++) {
        if(strcmp(string[1], "|") ==0){
            sector ++;
        }
    }
    
    if(sector) {
       temp = strtok(string, "|");
       while(temp) {
          line[i] = temp;
          i++;
          temp = strtok(NULL, "|");
      }
      printf("&#37;s", line[0]"); //returns hello -a
      for(i = 0; i <= count; i++) {
         temp = strtok(line[i], "");
         line2[i] = temp;
      }
      printf("%s", line[0]");  // returns hello
    }
    Last edited by cstudent; 04-23-2008 at 05:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 10:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM