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! |
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. |
I understand how to convert to a char*.
I guess my problem is that other than Code:
char* args[] = {"a", "b", "c"}; Code:
char* args[] = new char*[50] Basically, the lines with question marks confuse me: Code:
std::istringstream Stream(Proc); |
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. |
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.
|
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? |
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? |
Quote:
|
Ah. That makes sense.
|
Quote:
|
All times are GMT -5. The time now is 11:20 PM. |