Convert cstring to char *

Source: Internet
Author: User

1. Pass the const char * (lpctstr) pointer to the unallocated memory.
Cstring CSTR (asdd );
Const char * Ch = (lpctstr) CSTR;
Ch points to the same address as CSTR. However, since the use of const ensures that CH will not be modified, it is safe.

2. Pass the pointer to the unallocated memory.
Cstring CSTR = "asddsd ";
Char * Ch = CSTR. getbuffer (cstr1.getlength () + 1 );
CSTR. releasebuffer ();
// Modify the value that CH points to is equal to the value in CSTR.
// PS: After the CH is used up, the delete ch is not used, because this will destroy the internal space of CSTR and cause program crash.

3. Second usage. Assign the cstring value to the char * of the allocated memory *.
Cstring cstr1 = "asddsd ";
Int strlength = cstr1.getlength () + 1;
Char * pvalue = new char [strlength];
Strncpy (pvalue, cstr1, strlength );

4. Method 3: Assign the cstring value to the allocated memory char [] array.
Cstring cstr2 = "asddsd ";
Int strlength1 = cstr1.getlength () + 1;
Char charray [100];
Memset (charray, 0, sizeof (bool) * 100); // clears the junk content of the array.

Strncpy (charray, cstr1, strlength1 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.