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

  • SEARCH
  • Home
  • 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 9302847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:20:53+00:00 2026-06-18T23:20:53+00:00

SO, I am trying to write a function that will return a vector<char**> ,

  • 0

SO, I am trying to write a function that will return a vector<char**>, as such:

vector<char**> test(string mystr) {

  char*temp=new char[mystr.size()+1];

  strcpy(temp,mystr.c_str());

  char*subStr=strtok(temp,":");

  while(subStr!=NULL) {

    int i=0;

    char**args=new char*[200];

    char*tempsta=newchar[strlen(subStr)+1];

    strcpy(tempsta, subStr);

    args[i]=strtok(tempsta," ");

    while(args[i]!=NULL) {

      i++;

      args[i]=strtok(NULL," ");

    }

    fullVec.push_back(args);

    //cout<<subStr<<endl;

    subStr=strtok(NULL,":");

  }
return fullVec;
}

so I want to split the parameter string up with ":" delimeter, then with ” ” delimeter. On the cout<<subStr call I get what is expected if I comment out everything from int i=0 to fullVec.push_back(args). If I do not comment out all of those lines I only get the first substring (until the first “:”) is encountered, and then the largest while loop exits.

for what is expected I mean; let us assume the parameter is “my name is: bon jovi: xxx ab”
if everything is commented out, the following lines will be printed:

my name is
 bon jovi
 xxx ab

if I leave it as is, what will happen is only

my name is

will print, and the large loop will exit

any assistance is appreciated, thanks! (Yes, I am aware that this seems like a silly exercise which can be done much more elegantly/easily…however I would like to get this solution to work before I entertain using string etc.)

  • 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-06-18T23:20:54+00:00Added an answer on June 18, 2026 at 11:20 pm

    Your problem is that strtok() maintains state between calls.

    If the first parameter is not NULL then it uses to reset the state otherwise it uses the state it has saved to continue parsing from where it left off.

    Since you have two nested calls to strtok() the second call is messing with state of the outer call.

    This call:

    args[i]=strtok(tempsta," ");
    

    Is resetting the internal state of strtok(). It now no longer knows anything about the state from your outer call. Thus when you get to the end of the string in your inner loop.

    This call:

    subStr=strtok(NULL,":");
    

    Is now using the saved state of the inner loop. So it basically just terminates as you have already reached the end of that tokenizing stream.

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

Sidebar

Related Questions

I'm trying to write a function that will return all the available cars for
I am trying to write a function that will randomly return an (x,y) co-ordinates
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a small Powershell function that will return some summary data
I am trying to write a function that will test whether or not a
I'm trying to write a function in R that will return as its output,
I'm trying to write function similar to http://whatismyudid.com/ that, then approved, will return the
I'm trying to write a function that will return true or false each time
I'm trying to write a file upload function that will return either a true
I'm trying to write a function that will return the number of days between

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.