views:

370

answers:

7

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?

A: 

If str in your string use str.c_str() method to get the char* inside it.

Dror Helper
Are you sure it's `c_tsr` and not `c_str`? I'm no C++ expert but I Googled for `c_tsr` and was corrected to `c_str`.
Carl Smotricz
my bad - I'll fix that
Dror Helper
+17  A: 
  1. To get the C string equivalent of the C++ string object use c_str function.
  2. To locate the first occurence of a 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*.

codaddict
@unicornaddict: thanks. now how do I find the other occurrences of that character in the string? [the length of the string is huge, so creating a whole new string is not an option]
Amoeba
@cambr: There is a **overloaded** version of `find_first_of` which accepts the **position to start the search from** as an argument. I've posted an example here: http://www.ideone.com/bESwL
codaddict
@unicornaddict: thanks a lot!
Amoeba
+5  A: 

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.

anon
A: 

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!!

CVS-2600Hertz
Wrong. c_str() returns a const char *
anon
This isn't a very good idea. There's no guarantee that pw will point to the same data at a later point, even if some_string is still around. It's much safer to call c_str() to each function you need to pass a char*.
Justin Ardini
+1  A: 

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.

shoosh
It's not well defined.
anon
A: 
std::string yourString("just an example");
char* charPtr = new char[yourString.size()+1];
strcpy(charPtr, yourString.c_str());
JRL
Copying via an uninitialised pointer is rarely a good idea.
anon
@Neil: it's an example, jeez... but I've edited it for your pleasure ;-)
JRL
A: 

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;
}
gameover