Cstring to char *, const char *

Source: Internet
Author: User
Cstring to char *, const char *

 

In C, it indicates a string. It does not have the string concept. Only char * and char [] can be used. In MFC, to serve string operations, a class cstring is added, the header file of this class is afx. h.

The conversion from char * To cstring is simple. You only need to use the cstring constructor.

This document describes how to convert cstring to const char * and char.

The cstring class is more powerful than the string class of STL. when new users use cstring, they will be attracted by its powerful functions. however, due to lack of knowledge about its internal mechanism, it is easy for beginners to encounter many problems when converting a cstring to a C character array. because the cstring operator is already overloaded, it is not difficult to convert the cstring class to const char *, as shown below:
 

Char A [100];
Cstring STR ("aaaaaa ");
Strncpy (A, (lpctstr) STR, sizeof ());

Or:

Strncpy (A, STR, sizeof (a); flyhorse Note: The above statement sets the cstring --> const char *
Both methods are correct. because strncpy's second parameter type is const char *. therefore, the compiler automatically converts the cstring class to const char *. many people are confused about what the lpctstr is. Let's take a look:

1. LP indicates a long pointer. In Win16, there is a difference between a long pointer (LP) and a short pointer (P). In Win32, there is no difference, both 32-bit. therefore, the LP and P here are equivalent.

2. c Indicates const

3. t: What is it? We know that tchar is wchar_t when it is compiled in Unicode mode. If it is compiled into char in general mode, we can see that the length of lpctstr (pctstr) is const wchar_t * In uincode *, pcwstr, lpcwstr, Which is const char *, pcstr, and lpcstr in Multi-byte character mode. next we will look at how to convert a cstring to a char * in non-Unicode situations. Many beginners use the following methods for convenience:

(Char *) (lpcstr) Str

Note: If you only convert cstring --> char * without modifying the content of the string, this conversion is acceptable.
Is that true? Let's first look at an example:

Cstring STR ("AA ");
Strcpy (char *) (lpctstr) STR, "aaaaaaaa ");
Cout <(lpctstr) STR <Endl;
An exception occurs when running in debug. We all know that the cstring class has its own character pointer pointing to an allocated character buffer. if the number of characters written to it exceeds the buffer range, an exception may occur. but this program does not have problems in the release version. the original cstring class has been optimized. when the memory to be allocated is smaller than 64 bytes, the 64-byte memory is directly allocated, and so on. Generally, the size of the cstring character buffer is 64,128,256,512... this is to reduce the number of memory allocations and increase the speed.

Then someone will say that the number of characters I write to it does not exceed the original number of characters, so there will be no errors, such

Cstring STR ("aaaaaaa ");
Strcpy (char *) (lpctstr) STR, "AA ");
Cout <(lpctstr) STR <Endl;
It seems that there is no problem. Let's take a look at the example below:

Cstring STR ("aaaaaaa ");
Strcpy (char *) (lpctstr) STR, "AA ");
Cout <(lpctstr) STR <Endl;
Cout <Str. getlength () <Endl;
We can see that the length of STR has not changed, and continues to be 7 instead of 2. There are more serious problems:

Cstring STR ("aaaaaaa ");
Cstring str1 = STR;
Strcpy (char *) (lpctstr) STR, "AA ");
Cout <(lpctstr) STR <Endl;
Cout <(lpctstr) str1 <Endl;
We only changed STR, and str1 should have not changed, but they all changed to "AA" in real time ". is the character pointer in STR and str1 pointing to a buffer zone. we learned in Objective C ++ that if your class contains pointers, please write a copy constructor and a value assignment operator for your class. instead of directing the internal pointer of two objects to the same region, the memory should be re-allocated. is Microsoft making a mistake?

There is also a concept of "Copy at write time" and "reference count. the cstring class is widely used, which may generate a large number of temporary cstring objects in the system. in order to optimize the efficiency, the "Write-time replication" concept is widely used in the system software. that is, when a cstring generates another cstring, it does not copy its character buffer content, but adds the "reference count" of the character buffer to 1. when you need to rewrite the content in the character buffer, allocate the memory and copy the content. in the future, I will give an example of "Copy at write" and "reference count". We will return to the topic. When we need to convert cstring to Char
* What should we do? At that time, it was only a bit of trouble, as shown below:

Cstring STR ("aaaaaaa ");
Strcpy (Str. getbuffer (10), "AA ");
Str. releasebuffer ();

Note: Change cstring --> char * and modify the string content.
When we need a character array, call getbuffer (int n), where N is the length of the character array we need. call releasebuffer () immediately after use. It is also important to avoid using char * Where const char * can be used *

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.