CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Posts
    4

    converting string to c-string

    Hello,

    beginners question, what is the easiest way to convert a string (string class) to a c-string? Since declaring a c-string demands you it's size when declared, I'm not sure what the best way to do this is. Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: converting string to c-string

    If you want a const char* that points to the first char of a null terminated C-style string, use the c_str() member function of std::string.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2008
    Posts
    32

    Red face Re: converting string to c-string

    The following code should make the point clearer:


    #include <string>
    using namespace std;
    void main()
    {
    string str1("abc");

    printf("%s\n", str1.c_str());

    }

    The above program should print abc

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured