Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6774355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:46:37+00:00 2026-05-26T15:46:37+00:00

I found strtok function has some trick during you pass the same string to

  • 0

I found strtok function has some trick during you pass the same string to another function, code as follows:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void split_redirect(char input[80], int switch_bit);
void split_background(char *split_cmd);

void split_background(char *split_cmd){

    char *tmp = strchr(split_cmd, '&');
    if (tmp == NULL){ }
    else{
        char *command = strtok(split_cmd,"&");        
        while(command!=NULL){        
            // char *command_next = strtok(NULL,"&");
            printf("command=[%s]\n",command);

            if((strchr(command,'>')!=NULL)||(strchr(command,'<')!=NULL)){ 
                split_redirect(command,0);             
            }
            // command = command_next;
            command = strtok(NULL,"&");
            printf("command_next=[%s]\n",command);     
         }
    }
}

void split_redirect(char input[80], int switch_bit){
     char *tmp ,*tmp2;
     tmp = strchr(input, '>');
     tmp2 = strchr(input, '<');

     if (tmp == NULL && tmp2 == NULL){  }
     else if(tmp2 == NULL && tmp !=NULL){
     // single > 
        char *copy = (char *)malloc(sizeof(input));
        strcpy(copy,input);
        char *tmp = strtok(copy,">");
        char *cmd = tmp;
        tmp = strtok(NULL,"\0");

        if (strchr(tmp,'>')==NULL){
           char *file_name = strtok(tmp," ");
        }
        else{
           free(copy);
           copy=NULL;            
        }
    }
    else{ }
}

void main(int argc,char *argv[]){
    // char *cmd2 = "cmd1 > txt & cmd2 > txt2" // -> each char can't be change, so strotk this string will get seg fault. Modify token to '\0' is illegal. in .RDATA
    char cmd[80] = "cmd1 > txt & cmd2 > txt &"; 
    split_background(cmd);  
}

Expect output:
command=[cmd1 > txt ]
command_next=[ cmd2 > txt ]

The output:
command=[cmd1 > txt ]
command_next=[(null)]

Why when I pass this string to another function will cause the rest of string becomes null string? When I unmark char *command_next = strtok(NULL,"&"); and replace command = strtok(NULL,"&"); to command = command_next; It will print the rest of string as my expectation. Is it relate to how strtok store that string by static memory?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:46:38+00:00Added an answer on May 26, 2026 at 3:46 pm

    Read the manual: The strtok function changes the original string and overwrites the delimiter characters with null bytes. The rest of the string doesn’t “become null string”, but you have to resume the rest of the string one after the last found token.

    It’s probably best to use strtok idiomatically to extract all tokens, and then process those tokens individually.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
char *strtok(char *s1, const char *s2) repeated calls to this function break string s1
I found the code to convert a hexadecimal string into a signed int using
I found a weird problem when I was learning the strtok function. At first
Found a piece of code today, that I find a little smelly... TMyObject.LoadFromFile(const filename:
Found some examples of @NamedQuery annotations,e.g.: @NamedQuery(name=employeeBySsn query=select e from Employee e where e.ssn
Found the function which position the image(s) in the multiscaleimage... but I'm not sure
found a interesting problem during testing our web application. I have application on localhost
I'm trying to parse a string into smaller ones, extracting some values and then
found the following code: XmlDocument cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc); // retrieve the appSettings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.