18

Possible Duplicate:
Convert std::string to const char* or char*

Probably a & or something similar missing (I am noob at cpp).

I have

string R = "somthing"; char* S; 

How would I copy R into S

3
  • 1
    Why has this question been down voted twice? Commented Oct 12, 2012 at 15:54
  • @john: Maybe some people thought that this is a duplicate of another question, or they thought that this question shows no effort, I don't really know. For a beginner this isn't an obvious question, but I don't expect a comment from the downvoters in this case, since it's a duplicate. Commented Oct 12, 2012 at 15:59
  • 2
    @Zeta Maybe but the downvotes appeared before any indication of the duplication. Seems like bad manners to me. Commented Oct 15, 2012 at 11:39

2 Answers 2

45

There are many ways. Here are at least five:

/* * An example of converting std::string to (const)char* using five * different methods. Error checking is emitted for simplicity. * * Compile and run example (using gcc on Unix-like systems): * * $ g++ -Wall -pedantic -o test ./test.cpp * $ ./test * Original string (0x7fe3294039f8): hello * s1 (0x7fe3294039f8): hello * s2 (0x7fff5dce3a10): hello * s3 (0x7fe3294000e0): hello * s4 (0x7fe329403a00): hello * s5 (0x7fe329403a10): hello */ #include <alloca.h> #include <string> #include <cstring> int main() { std::string s0; const char *s1; char *s2; char *s3; char *s4; char *s5; // This is the initial C++ string. s0 = "hello"; // Method #1: Just use "c_str()" method to obtain a pointer to a // null-terminated C string stored in std::string object. // Be careful though because when `s0` goes out of scope, s1 points // to a non-valid memory. s1 = s0.c_str(); // Method #2: Allocate memory on stack and copy the contents of the // original string. Keep in mind that once a current function returns, // the memory is invalidated. s2 = (char *)alloca(s0.size() + 1); memcpy(s2, s0.c_str(), s0.size() + 1); // Method #3: Allocate memory dynamically and copy the content of the // original string. The memory will be valid until you explicitly // release it using "free". Forgetting to release it results in memory // leak. s3 = (char *)malloc(s0.size() + 1); memcpy(s3, s0.c_str(), s0.size() + 1); // Method #4: Same as method #3, but using C++ new/delete operators. s4 = new char[s0.size() + 1]; memcpy(s4, s0.c_str(), s0.size() + 1); // Method #5: Same as 3 but a bit less efficient.. s5 = strdup(s0.c_str()); // Print those strings. printf("Original string (%p): %s\n", s0.c_str(), s0.c_str()); printf("s1 (%p): %s\n", s1, s1); printf("s2 (%p): %s\n", s2, s2); printf("s3 (%p): %s\n", s3, s3); printf("s4 (%p): %s\n", s4, s4); printf("s5 (%p): %s\n", s5, s5); // Release memory... free(s3); delete [] s4; free(s5); } 
Sign up to request clarification or add additional context in comments.

1 Comment

c_str() creates const char * rather than char *
19

First of all, you would have to allocate memory:

char * S = new char[R.length() + 1]; 

then you can use strcpy with S and R.c_str():

std::strcpy(S,R.c_str()); 

You can also use R.c_str() if the string doesn't get changed or the c string is only used once. However, if S is going to be modified, you should copy the string, as writing to R.c_str() results in undefined behavior.

Note: Instead of strcpy you can also use str::copy.

1 Comment

<string>.c_str() is great for reading the string as a char* type just once for some sort of processing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.