-3

i transform char* to char**. Each word must fold to array. But fold only first word. input: "abc def 123" out (expected): num ==3,arr == {"abc","def","123"} out(real): num == 1,arr == {"abc"}

struct CommandArray
{
    vector<char*> Arr;
    USHORT Num;
};
CommandArray Text::StrToArray(LPSTR Str)
{
    CommandArray Out;
    LPSTR TempStr;
    Out.Num = 0;
    TempStr = strtok (Str," ");
    while(TempStr != NULL)
    {
        Out.Arr.push_back(TempStr);
        Out.Num++;
        TempStr = strtok(NULL," ");
    }
    return Out;
}
bagi
  • 21
  • 2
  • 8
  • 5
    Care to elaborate, and possibly making this into a proper question? Can you please describe what's wrong, what's expected to happen, expected output for some specific input, etc. – Some programmer dude Aug 20 '12 at 11:58
  • 1
    [How do I tokenize a string in C++](http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c) – jrok Aug 20 '12 at 12:00
  • Joachim, input: "abc def 123", out (expected): num ==3,arr == {"abc","def","123"}, out(real): num == 1,arr == {"abc"} – bagi Aug 20 '12 at 12:03
  • You can still get a `char` pointer from a `std::string` with `std::string::c_str()`. Also, you don't need to keep track of the number yourself, `std::vector::size()` returns the number of entries in the vector. – Some programmer dude Aug 20 '12 at 12:04
  • 1
    Please add any additional information to the question itself. Update it. Don't hide it within the comments. – Bart Aug 20 '12 at 12:06
  • 1
    The code works as expected when used properly: http://ideone.com/IdKou. The problem must be elsewhere. – j_random_hacker Aug 20 '12 at 12:09
  • Also: the code as it stands is fragile because the calls to `strtok()` manipulate global state, meaning this function can't be called reentrantly (e.g. from signal handlers) or multiple threads. Also there is no need to store `CommandArray::Num` as the info is already in `CommandArray::Arr.size()` (i.e. you could just use the vector and forget about the `CommandArray` struct wrapping it). – j_random_hacker Aug 20 '12 at 12:13
  • This code should compile, but i would change at least one thing: try to not put pointers into std::vector, there is boost::ptr_vector. It will make it really easier. If you are in c++11 world, try to use emplace_back or pass string via reference. – CyberGuy Aug 20 '12 at 12:44

2 Answers2

0

strtok modifies its first argument (that's why it is a char* and not a char const*). One guess is that you do the same after the call to Text::StrToArray.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
0

First. Use cin.getline(input,32); to get an input seperated by spaces, cin>>input won't work.

Concerning char* to char**. The code below folds each word into an array.

#include<windows.h>
#include<vector>
#include<string>
#include<iostream>
using namespace std;


struct CommandArray
{
    vector<char*> Arr;
    USHORT Num;
};


CommandArray  StrToArray(LPSTR Str)
{
    CommandArray Out;
    LPSTR TempStr;
    Out.Num = 0;
    TempStr = strtok (Str," ");
    while(TempStr != NULL)
    {
        Out.Arr.push_back(TempStr);
        Out.Num++;
        TempStr = strtok(NULL," ");
    }
    return Out;
}


int main()
{

    int ROWS=80; //80 characters wide
    int COLUMNS=20;// 20 lines
    int i;

    char seperators[]   = " ,\t\n";
    char *token;    

    char* input_Dynamic1DCharPointerArray = new char[80];
    char **output_Dynamic2DCharPointerArray = 0;
    //memory allocated for elements of rows.
    output_Dynamic2DCharPointerArray = new char *[ROWS] ;
    //memory allocated for  elements of each column.
    for(   i = 0 ; i < ROWS ; i++ ) output_Dynamic2DCharPointerArray[i] = new char[COLUMNS];


    strcpy(input_Dynamic1DCharPointerArray,"apples 123 oranges 456 bananas 789 lemons 101112" );

    //cout<<"  \n";
    //cin.getline(input_Dynamic1DCharPointerArray,32);

    cout<<"  \ninput = "<<input_Dynamic1DCharPointerArray<<"  \n\n";
    cout<<"Output = \n";

   token = strtok( input_Dynamic1DCharPointerArray, seperators );  
   i=0;
   while( token != NULL )
   {
      strcpy(output_Dynamic2DCharPointerArray[i],token);

      cout<<output_Dynamic2DCharPointerArray[i]<<"  \n";

      token = strtok( NULL, seperators ); // C4996
      i++;
   }

    cout<<"  \n";
    delete[] input_Dynamic1DCharPointerArray;

    //free the allocated memory
    for(   i = 0 ; i < ROWS ; i++ )
    delete [] output_Dynamic2DCharPointerArray[i] ;
    delete [] output_Dynamic2DCharPointerArray ;

    return 0;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28