string object to null-terminating string conversion

This is a discussion on string object to null-terminating string conversion within the C++ Programming forums, part of the General Programming Boards category; Hello anybody out there. I have a problem understanding the function to convert string object to null-terminated string. The function ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    4

    string object to null-terminating string conversion

    Hello anybody out there. I have a problem understanding the function to convert string object to null-terminated string.

    The function looks something like:

    const char *c_str()const;

    my program looks something like this:

    char word[10]; // null-terminated string (array of char)
    string word2; // string object

    cin >> word2;

    word = word2; // conversion done here; error!!

    cout << word;

    How do I apply the conversion function here?

  2. #2
    William
    Guest
    Why don't you just use word all along?. Whatever, using c_str() is done like this :

    std::string str;
    const char* txt=str.to_string();

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    remember you cannot use the assignment operator with c_style strings. So if you want to assign the value of word2 to word and you do not initialize word with word2, as in William's example, you need to use strcpy():

    strcpy(word, word2.c_str());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 12:16 PM
  3. Syntax Error??
    By Kennedy in forum C Programming
    Replies: 8
    Last Post: 09-06-2006, 12:04 PM
  4. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  5. Why am I getting these errors??
    By maxthecat in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2006, 01:00 PM

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