Hi Im trying to put a string in a character array like this
string username;
char Uname[30];
I tryed string copy and didnt work, what other functions are good for this? I checked the tutorials but didnt find anything.
Hi Im trying to put a string in a character array like this
string username;
char Uname[30];
I tryed string copy and didnt work, what other functions are good for this? I checked the tutorials but didnt find anything.
>Hi Im trying to put a string in a character array like this
Code:#include <cstring> using namespace std; . . strcpy(Uname, username.c_str());
Wow, like how did u know what? Where do I read to learn these things?
You could try this:
String objects can act like char arrays.Code:int i = 0; for(; username[i]; i++) { Uname[i] = username[i]; } Uname[i] = '\0';
Edit: I've been beaten, but this is the way without functions.
Last edited by homeyg; 05-24-2005 at 03:27 PM.
>Wow, like how did u know what? Where do I read to learn these things?
Here's a good online reference for the C and C++ standard libraries:
www.cppreference.com
The above also has good information on the STL, but there's also:
www.sgi.com/tech/stl/
Nice Links!
> Wow, like how did u know what? Where do I read to learn these things?
You learn through experience and documentation about the different functions available for each datatype you use. You now know that the string class has a function called c_str() that can be used when you need a const char*.
homeyg, using strcpy would be better in this case, especially since your loop ignores the length of the string and instead just copies 30 characters. The string could very easily not have a null character at the end of it in memory, which would make your loop cause a crash.
Of course even swoopy's suggestion will lead to bad things if the username string is longer than 29 characters.
Also if you ever have to go the other way (char array to string), you can do:
Code:username = Uname;
Check the code, I just realized that and fixed it.Originally Posted by Daved
> Check the code, I just realized that and fixed it.
Sorry, but you didn't. The username string variable isn't guaranteed to end with a null character. Your loop might go on forever. Use the length() or size() function, or make that test with the return value to c_str(), or even better, just use strcpy and save the hassle.
I thought that string objects automatically add the null character. Is this not true?Originally Posted by Daved
I guess this is not so.
> I thought that string objects automatically add the null character. Is this not true?
Internally you don't know how the string object maintains the string, but they are not null terminated. So if the string str = "hello", then all you know is that str[0] is 'h', str[1] is 'e', ..., and str[4] = 'o'. Checking str[5] is looking past the bounds of the string and invokes undefined behavior. If you call c_str(), then a const char* is returned that does have the null character at the end. This might be done by always keeping a null character after the last character in memory, but it might not, so you cannot count on it.
Also you can learn a lot from reading the threads here a Cprogramming. And some good books are invaluable. One that's highly recommended is "Accelerated C++". And there's one about the STL (Standard Template Library) too, but I can't remember its name.
I guess I should add some error checking.
You could use sizeof(Uname) to get the size, but then if you put this in a function, sizeof() no longer works.Code:#include <cstring> #include <string> using namespace std; const uname_len_max = 30; int main() { string username("...................."); char Uname[uname_len_max]; if (username.length() < uname_len_max) strcpy(Uname, username.c_str()); else cout << "String too long to copy." << endl; }
Last edited by swoopy; 05-24-2005 at 03:50 PM.
You could also use strncpy to just copy as much as will fit in the target C string and add the null terminator afterwards.
> I can't remember its name.
Maybe you are thinking of The C++ Standard Library by Josuttis.