LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ std::string tokens to char*[] (https://www.linuxquestions.org/questions/programming-9/c-std-string-tokens-to-char%2A%5B%5D-683009/)

nonis 11-12-2008 08:49 PM

C++ std::string tokens to char*[]
 
I want to convert by token a std::string into a char*[] (where each token is a char* in the array).

I seem to be having problems with the syntax. It's confusing me. =P
Does anyone know how to do this?

I can use a stringstream to tokenize, but I'm not sure how to initialize the array, or how to assign array elements to the tokens.

I basically want to create a new process after forking with execvp, but it requires a char*[]. If there's a cleaner way to do this, please tell!

pinniped 11-12-2008 10:58 PM

man std::string

That should tell you something about:

std::string.c_str()

Well, of course I'm assuming you installed the C++ stdlib docs.

nonis 11-12-2008 11:24 PM

I understand how to convert to a char*.

I guess my problem is that other than
Code:

char* args[] = {"a", "b", "c"};
I don't know how to create a char*array.

Code:

char* args[] = new char*[50]
or something doesn't work.

Basically, the lines with question marks confuse me:

Code:

        std::istringstream Stream(Proc);

        char *args[] = ???; //Initializing args
        int Count = 0;
        while (Stream)
        {
                std::string Token;
                Stream >> Token;
                args[Count]??? = Token.c_str(); // Assigning an element of args
                Count++;
        }


pinniped 11-13-2008 12:08 AM

Oh, OK. Well, in the code example you gave, *you* are the one responsible for allocating memory for 'args' and initializing it (and later freeing it). Also the declaration doesn't make sense; you are requesting a static allocation but have not specified the size; try "char **args" instead. You will then have to allocate space for 'args' to store the list of char pointers; keep in mind that pointer sizes can vary from one machine to another (or even on the same machine), so you need to allocate memory according to the size of char* as determined at run-time (actually at compile-time is sufficient if you know what's going on).

Otherwise, your later line with the '???' doesn't have anything wrong with it. You just have to be careful with overrunning your allocated memory etc.

I won't comment on the rest of the code in general; I'm assuming that is only for explaining your problem and not actual production code.

nonis 11-13-2008 01:26 AM

I understand this. I'm just not quite sure about how to allocate memory for each of the array elements and the array itself. The syntax is what I'm not sure about.

pinniped 11-13-2008 02:29 AM

Maybe this would help you:

http://publib.boulder.ibm.com/infoce...c05cplr199.htm

If you choose to use 'malloc' then it's simply:

char **blah = malloc(sizeof(char *)*ARRAY_SIZE);

If you want to use the new() operator:

char **blah = ::new char *[ARRAY_SIZE];
(and to delete: ::delete [] blah; )

As in C, those strange sequences of ()* and so on can get confusing; you just have to get used to the rules on precedents. Most of the time you can use new/delete naked, but in some cases you do need to use ::new/::delete

[edit] Your assignment will upset the compiler though since c_str() returns 'const CharT *' not 'char *'. On top of that, what is CharT? Is it ASCII or unicode or UTF-8?

nonis 11-13-2008 02:50 AM

Thanks. That actually helps a lot. I just couldn't wrap my mind around the sizeof() and stuff. And I wasn't sure how to do it with new(). It's late. I probably should be sleeping and working on it in the morning when I can actually read correctly.

I've never seen ::new used. When do you need it?

pinniped 11-13-2008 03:04 AM

Quote:

Originally Posted by nonis (Post 3340340)
I've never seen ::new used. When do you need it?

If new() is redefined within a class, every time you use new() within that class, the new definition is used. To force the compiler to use the usual definition you need to specifically request it with '::new()'. This is similar to other name/scope issues which arise with the C++ object model.

nonis 11-13-2008 04:26 PM

Ah. That makes sense.

dmail 11-14-2008 08:00 AM

Quote:

I won't comment on the rest of the code in general; I'm assuming that is only for explaining your problem and not actual production code.
I would just like to point out that using a std::vector will remove some of the allocations from user code and that using the pointer return by c_str() in the manner which you do is error prone for reasons such as the string going out of scope.


All times are GMT -5. The time now is 11:20 PM.