I have a C++ string
. I need to pass this string to a function accepting char*
parameter,
for example - strchr()
.
a) How do I get that pointer?
b) Is there some funtion equivalent to strschr()
that works for C++ strings
?
I have a C++ string
. I need to pass this string to a function accepting char*
parameter,
for example - strchr()
.
a) How do I get that pointer?
b) Is there some funtion equivalent to strschr()
that works for C++ strings
?
If str in your string use str.c_str()
method to get the char* inside it.
C
string equivalent of
the C++
string
object use c_str
function.char
in a string
object use
find_first_of
function.Example:
string s = "abc";
// call to strlen expects char *
cout<<strlen(s.c_str()); // prints 3
// on failure find_first_of return string::npos
if(s.find_first_of('a') != string::npos)
cout<<s<<" has an a"<<endl;
else
cout<<s<<" has no a"<<endl;
Note: I gave the strlen
just an example of a function that takes char*
.
Surprisingly, std:;string has far, far more capabilities than C-style strings. You probably want the find_first_of() method. In general, if you find yourself using the strxxx() functions on C++ std::strings, you are almost certainly doing something wrong.
Like much of the C++ Standard Library, the string class is a complex beast. To make the most of it, you really need a good reference book. I recommend The C++ Standard Library, by Nicolai Josuttis.
If you just want to assign a string literal to pw, you can do it like
char *pw = "Hello world";
If you have a C++ std::string object, the value of which you want to assign to pw, you can do it like
char *pw = some_string.c_str()
However, the value that pw points to will only be valid for the life time of some_string.
More here :
http://stackoverflow.com/questions/2235366/how-to-assign-a-string-to-char-pw-in-c
GoodLUCK!!
You can't get a char*
from a string
string
does not allow you free access to its internal buffer.
The closest you can get is a const char*
using .c_str()
if you want it null terminated or .data()
if it doesn't have to be null terminated.
You can then cast the pointer returned by these functions to char*
but you do this on your own risk. That being said this is a relatively safe cast to make as long as you make sure you're not changing the string. If you changed it then the pointer you got from c_str()
may no longer be valid.
This code:
string str("Hello World!");
char* sp = (char*)str.c_str();
sp[5] = 'K';
is probably ok
However this:
string str("Hello World!");
char* sp = (char*)str.c_str();
str = "Chaged string";
sp[5] = 'K';
is most definitely not ok.
std::string yourString("just an example");
char* charPtr = new char[yourString.size()+1];
strcpy(charPtr, yourString.c_str());
Perhaps this exmaple will help you
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Replace the vowels in this sentence by asterisks.");
size_t found;
found=str.find_first_of("aeiou");
while (found!=string::npos)
{
str[found]='*';
found=str.find_first_of("aeiou",found+1);
}
cout << str << endl;
return 0;
}