String to a character array?

This is a discussion on String to a character array? within the C++ Programming forums, part of the General Programming Boards category; Hi Im trying to put a string in a character array like this string username; char Uname[30]; I tryed string ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    110

    String to a character array?

    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.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Hi Im trying to put a string in a character array like this
    Code:
    #include <cstring>
    using namespace std;
    .
    .
    strcpy(Uname, username.c_str());

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Wow, like how did u know what? Where do I read to learn these things?

  4. #4
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    You could try this:

    Code:
    int i = 0;
    for(; username[i]; i++)
    {
         Uname[i] = username[i];
    }
    
    Uname[i] = '\0';
    String objects can act like char arrays.

    Edit: I've been beaten, but this is the way without functions.
    Last edited by homeyg; 05-24-2005 at 03:27 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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/

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Nice Links!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    > 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.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Also if you ever have to go the other way (char array to string), you can do:
    Code:
    username = Uname;

  9. #9
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by Daved
    > 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.
    Check the code, I just realized that and fixed it.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    > 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.

  11. #11
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote 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.
    I thought that string objects automatically add the null character. Is this not true?

    I guess this is not so.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    > 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.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I guess I should add some error checking.
    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;
    }
    You could use sizeof(Uname) to get the size, but then if you put this in a function, sizeof() no longer works.
    Last edited by swoopy; 05-24-2005 at 03:50 PM.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. How to convert a string to a character array?
    By Aven in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2005, 11:14 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21