CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 1999
    Posts
    2

    Converting CStrings to char*

    How do I convert from a CString to a character array.

    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    ie:
    char* string1;
    CString string2("Hello");
    /*
    How would I go about putting Hello
    into string1?
    */
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    Thanks,
    Phil



  2. #2
    Join Date
    May 1999
    Posts
    82

    Re: Converting CStrings to char*

    Try this...

    char* string1;
    CString string2("Hello");
    string1 = string2.GetBuffer(-1);


  3. #3
    Join Date
    May 1999
    Posts
    9

    Re: Converting CStrings to char*

    char* string1;
    CString string2("Hello");
    // Try this...
    string1 = new char[string2.GetLength()];
    strcpy(string1,(LPCTSTR)string2);

    this should work....


    nms

  4. #4
    Join Date
    Apr 1999
    Posts
    26

    Re: Converting CStrings to char*

    char* s1;
    CString s2 ("Hello");

    s1 = (char*)(const char*)s2;
    // This will xfer the contents of s2 to s1

    Cheers!
    Chandra



  5. #5
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Converting CStrings to char*

    I'd prefer this one:

    char* string1 = NULL;
    CString string2("Hello");
    // Try this...
    string1 = new char[string2.GetLength() + 1];
    ::strcpy(string1, string2);

    Sally


  6. #6
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Converting CStrings to char*

    Nope it won't. it will make s1 POINT to s2 which is different

    Sally


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