LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2008, 08:49 PM   #1
nonis
Member
 
Registered: Jan 2007
Posts: 95

Rep: Reputation: 15
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!
 
Old 11-12-2008, 10:58 PM   #2
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
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.
 
Old 11-12-2008, 11:24 PM   #3
nonis
Member
 
Registered: Jan 2007
Posts: 95

Original Poster
Rep: Reputation: 15
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++;
	}
 
Old 11-13-2008, 12:08 AM   #4
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
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.
 
Old 11-13-2008, 01:26 AM   #5
nonis
Member
 
Registered: Jan 2007
Posts: 95

Original Poster
Rep: Reputation: 15
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.
 
Old 11-13-2008, 02:29 AM   #6
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
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?

Last edited by pinniped; 11-13-2008 at 02:35 AM.
 
Old 11-13-2008, 02:50 AM   #7
nonis
Member
 
Registered: Jan 2007
Posts: 95

Original Poster
Rep: Reputation: 15
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?
 
Old 11-13-2008, 03:04 AM   #8
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Quote:
Originally Posted by nonis View Post
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.
 
Old 11-13-2008, 04:26 PM   #9
nonis
Member
 
Registered: Jan 2007
Posts: 95

Original Poster
Rep: Reputation: 15
Ah. That makes sense.
 
Old 11-14-2008, 08:00 AM   #10
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C function to split a string in tokens rogx Programming 16 08-06-2008 11:33 AM
c++ - error: cannot convert ‘std::basic_string<char, babag Programming 5 05-15-2008 11:50 AM
bash - getting tokens from string klavuzkarga Linux - Newbie 6 12-14-2007 01:13 PM
std::string in write lucky6969b Programming 2 12-20-2005 10:53 PM
C++ std::string to int Slaxx Programming 1 10-30-2004 10:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration