string - char *solved*

Started by
7 comments, last by evolutional 19 years, 6 months ago
how would you do this (is there a pre-made functino for this) if not what kind of things could i do to store what a string = into a char whatever[]; [Edited by - raptorstrike on September 24, 2004 2:42:02 PM]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
You mean like:

string MyString = "Stuff";
cout << MyString.c_str() << endl;

Or do you mean something else?
Constant string:
char msg[] = "Message";   // Size is automatic


Pointer to string:
char msg[256];  // MUST be large enough to hold text plus one byte!char *pString = "Message";strcpy(msg, pString);


C++ string:
char msg[256];  // Again, must have enough roomstd::string str("Message");strcpy(msg, str.c_str());

no i mean like

string something = "";
char whatever[6] = something;

oops just saw previous post
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
no you can't do that. You are assigning the pointer. Overload the '=' operator to do strcpy or something.
--------------------------------------------------------Life would be so much easier if we could just get the source code.
i know i cant do that thats why this thread is here i just want to know how to get something to that effect
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Ah, then like it was said above use the method strcpy on 2 char arrays, or use the = operator on 2 std:string (make sure these are NOT pointers)
--------------------------------------------------------Life would be so much easier if we could just get the source code.
ok thanks alot you guys

wait from what i can tell im assigning

charArray[] to something

i want to do it the other way around

this is what i have so far

char scorestore[6];
char *pPrint;
string Print = strcpy(scorestore, pPrint);
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Technically, the call to the std::string.c_str() method will give you access to the raw character bytes in the string.

You can then use this method to copy a std::string to a char[] array using the strcpy() function. Just make sure that you allocate enough memory for the copy.

This topic is closed to new replies.

Advertisement