LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-12-2006, 08:08 PM   #1
anamericanjoe
Member
 
Registered: May 2006
Posts: 69

Rep: Reputation: 16
Convert C++ String Vector to char array


I have a vector of strings that I need to convert to a C string. I am trying to perform said conversion like so:

Code:
vector<string> tokens;
char* args;     
/*
 *  Fill the vector with some strings... 
 */                                                                                                           
args = new char [tokens.size()];                                                                                           
copy( tokens.begin(), tokens.end(), args);
I get some wild errors when it comes time to compile. Why doesn't this work? Is there a better way to do this?
 
Old 12-12-2006, 09:29 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
A char* is not an array or strings, it's a pointer to a char. Strings in C are just a pointer to a char where subsequent addresses in memory contain the nect characters of the string. In the case of the str* functions in the standard C library, and in many other places according to convention, the string ends when a \0 (ASCII NUL) character is found. The programmer is responsible for memory management with such strings.

An "array of strings" in C is a pointer to a pointer to a char, like argv (type = char**). Normally the programmer must allocate memory for the array of pointers, and also allocate memory for each string which is pointed to.

Enter the STL string class... It's all fine, but it gets ugly when youo want to mix it with old C style strings. There is a member of the class, c_str() which returns char* (don't free() this!). If you want to modify this, you should allocate for your own char*, and copy it with strcpy().
 
  


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# convert char array to string exodist Programming 3 09-16-2008 08:06 AM
How to convert "string" type to "char * " in c++ pranavojha Programming 18 06-05-2008 03:10 PM
Convert 64 bit integer to char array in C++ syseeker Programming 2 06-27-2006 03:33 AM
How to convert string to char? twirl Programming 27 10-13-2005 07:11 AM
Convert C++ string to C char* nyk Programming 3 06-17-2004 08:15 AM

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

All times are GMT -5. The time now is 11:17 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