Thread: string object to null-terminating string conversion

  1. #1
    Registered User
    Join Date
    Oct 2001

    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
    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
    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, 03:45 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Syntax Error??
    By Kennedy in forum C Programming
    Replies: 8
    Last Post: 09-06-2006, 11:04 AM
  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