string to char*

if I have:
string line = "ABCDE";
and I want to break this down to A,B,C,D,E where each is a char.
I've tried this.
1
2
3
4
char *lnPtr = new char[line.length() + 1];
for(int x=0; x<line.length();x++) {
lnPtr[x] = line.substr(x,1); // error..cannot convert to char
}


How could I do this ?
thats what a string is, an array of characters?

you can access a c++ string like so
1
2
std::string str( "abcd" );
std::cout << str[ 0 ];


which would output
a
line[x] where x is an integer.
or
line.at(x)

See here for string reference:
http://www.cplusplus.com/reference/string/string/
Last edited on
why are you trying to do this in the first place?

another function string has is returning the string value as a c-string

string.c_str();
According to the C++98 standard:
A program shall not alter any of the characters in this sequence.

http://www.cplusplus.com/reference/string/string/c_str/

A good reason I can think of would be to have a mutable character sequence.
Another reason would be if nmn did not know about the [] operator.
Member function substr returns an object of type std::string while you need a simple character. Use the following code


1
2
3
4
5
6
7
8
9
10
char *lnPtr = new char[line.length() + 1];

std::string::size_type i = 0;

for( ; i < line.length(); i++ ) 
{
   lnPtr[i] = line[i];
}

lnPtr[i] = '\0';
The simplest way is to use standard function std::strcpy from <cstring>

1
2
3
char *lnPtr = new char[line.length() + 1];

std::strcpy( inPtr, line.c_str() );
The below post is completely off topic and only meant to be a small disclaimer.
I want the OP and any other beginner that comes here to realize strcpy can be unsafe, if not used properly.
(See here for a definition of buffer overflow error: http://www.cs.wright.edu/~pmateti/InternetSecurity/Lectures/BufferOverflow/ )

A word of caution to anyone who uses strcpy, it is an unbounded string function. It could be susceptible to overflow and buffer attacks.
Functions that have a length bound are safer.
For example:
http://www.cplusplus.com/reference/string/string/copy/

Edit: The key word is susceptible. The possibility is actually very simple. Example posted earlier is safe providing it is not a part of a larger system. Unbounded functions can lead to vulnerabilities.
Simple examples:
http://www.cse.scu.edu/~tschwarz/coen152_05/Lectures/BufferOverflow.html
http://netsec.cs.northwestern.edu/readings/buffer-overflows/
http://stackoverflow.com/questions/3026361/performing-simple-buffer-overflow-on-mac-os-10-6

And if anyone likes online research papers:
https://www.usenix.org/legacy/event/sec08/tech/full_papers/dalton/dalton_html/
Last edited on
I don't see any problem with posted example, it could not cause any buffer overflow, no matter what.

I don't see any reason to use a char array here.

std::string already lets you access individual chars as Paoletti illustrated in the first reply.

The rest of this talk about strcpy/c_str is noise.
Last edited on
Topic archived. No new replies allowed.