String and char* compatibility

This is a discussion on String and char* compatibility within the C++ Programming forums, part of the General Programming Boards category; If I use string class for all strings in my code, how do I ensure compatibility with functions (e.g. math ...

  1. #1
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96

    String and char* compatibility

    If I use string class for all strings in my code, how do I ensure compatibility with functions (e.g. math - atof()) that require char* type? In case of atof(), I use this: "atof(&thestring[0])". It works, because atof() has parameter as const, it doesn't change it. But if there is any other function with non-const char* parameter, how do I properly pass a string (from string class) to it?
    Please excuse my poor english...

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    To convert strings to const char*'s you should use the member function c_str() to guarantee portability.

    Unless you copy the const char* returned from c_str() into a char array first, modify that and pass it back into the string, you can't directly pass a string object into a function that modifies a char[]. Depending on what you are trying to achieve there may be a function that does what you want using a string rather than a character array.
    zen

  3. #3
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    thanx

    (I've heard about c_str() before, but never used it)
    Please excuse my poor english...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  2. Dynamic filenames?
    By Callith in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2004, 02:14 PM
  3. exe files in -c- language
    By enjoy in forum C Programming
    Replies: 6
    Last Post: 05-18-2004, 05:36 PM
  4. better c string functions
    By samps005 in forum C Programming
    Replies: 8
    Last Post: 11-04-2003, 01:28 PM
  5. Swapping string char with pointers
    By Black-Hearted in forum C++ Programming
    Replies: 4
    Last Post: 06-18-2003, 06:36 AM

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