Annoying various characters and character pointer types in Windows programming

Source: Internet
Author: User

In Windows programming, these data types are easy to see: Lpstr,lptstr,lpctstr ... Like a lot of children's shoes, at the time of learning Windows programming, the data type is really the two monks, can not touch the brain, long time will not remember. So, here's a summary of this.

First, let's look at the difference between ASCII encoding and Unicode encoding. The following is a description of Unicode encoding in the book "Windows Programming":

Simply put, Unicode extends from the ASCII character set. In strict ASCII, each character is represented by 7 bits, or 8 bits per character commonly used on the computer, and Unicode uses a full 16-bit character set.

This enables Unicode to represent characters, glyphs, and other symbols that may be used for computer communication in all the written languages of the world. Unicode was originally intended as a supplement to ASCII, if possible,

will eventually replace it. Given that ASCII is the most dominant criterion in a computer, this is a very high goal indeed.

Visible, the difference between the two is that ASCII-encoded characters (Chrater) occupy one byte (byte) in memory, and one character in Unicode-encoded characters occupies two bytes.

Next, let's look at some of the common data types in Windows. The definition of the data type in Windows is in the header file "Windef.h" and "winnt.h":

1 //windef.h2typedef unsignedLongULONG;3typedef ULONG *Pulong;4typedef unsigned ShortUSHORT;5typedef USHORT *Pushort;6typedef unsignedCharUCHAR;7typedef UCHAR *Puchar;8typedefChar*PSZ;9 Ten typedef UINT WPARAM; One typedef LONG LPARAM; A typedef LONG LRESULT; -  -typedef unsignedLongDWORD; thetypedefintBOOL; -typedef unsignedCharBYTE; -typedef unsigned ShortWORD; -typedeffloatFLOAT; +typedef FLOAT *pfloat; -typedef BOOL NEAR *Pbool; +typedef BOOL FAR *Lpbool; Atypedef BYTE NEAR *pbyte; attypedef BYTE FAR *Lpbyte; -typedefintNear *PINT; -typedefintFAR *Lpint; -typedef WORD NEAR *Pword; -typedef WORD FAR *Lpword; -typedefLongFAR *Lplong; intypedef DWORD NEAR *Pdword; -typedef DWORD FAR *Lpdword; totypedefvoidFAR *lpvoid; +typedef CONSTvoidFAR *lpcvoid; -  thetypedefintINT; *typedef unsignedintUINT; $typedef unsignedint*Puint;Panax Notoginseng  - //winnt.h thetypedefCharCHAR; +typedef ShortShort ; AtypedefLongLONG; the  + #ifndef _mac -   $typedef unsigned Shortwchar_t; $ typedef wchar_t WCHAR;  -  -typedef WCHAR *Pwchar; thetypedef WCHAR *LPWCH, *Pwch; -typedef CONST WCHAR *lpcwch, *Pcwch;Wuyitypedef WCHAR *Nwpstr; thetypedef WCHAR *LPWSTR, *Pwstr; -  Wutypedef CONST WCHAR *LPCWSTR, *Pcwstr; -  About // $ //ANSI (multi-byte Character) Types - // -typedef CHAR *PCHAR; -typedef CHAR *LPCH, *PCH; A  +typedef CONST CHAR *LPCCH, *pcch; thetypedef CHAR *Npstr; -typedef CHAR *LPSTR, *PSTR; $typedef CONST CHAR *LPCSTR, *Pcstr; the  the // the //Neutral ansi/unicode types and macros the // -#ifdef UNICODE//r_winnt in  the #ifndef _tchar_defined thetypedef WCHAR TCHAR, *Ptchar; Abouttypedef WCHAR TBYTe, *Ptbyte; the #define_tchar_defined the #endif/*!_tchar_defined */ the  + typedef LPWSTR Lptch, Ptch; - typedef LPWSTR PTSTR, LPTSTR; the typedef LPCWSTR LPCTSTR;Bayi typedef LPWSTR LP; the #define__text (quote) l# #quote//r_winnt the  - #else/* UNICODE *///r_winnt -  the #ifndef _tchar_defined thetypedefCharTCHAR, *Ptchar; thetypedef unsignedCharTBYTe, *Ptbyte; the #define_tchar_defined - #endif/*!_tchar_defined */ the  the typedef LPSTR Lptch, Ptch; the typedef LPSTR PTSTR, LPTSTR;94 typedef LPCSTR LPCTSTR; the #define__text (quote) quote//r_winnt the  the #endif/* UNICODE *///r_winnt98 #defineTEXT (quote) __text (quote)//r_winnt About  - 101typedef short *Pshort; 102typedef LONG *plong;
View Code

From these type definitions and macro definitions, it is easy to see that Windows is basically replaced with uppercase letters.

In Windows programming, the most common is the article at the beginning of those data types mentioned, in fact, carefully observed, it is not difficult to find that LP stands for "Long Pointer", and a long pointer, W for wide, wide byte, C for const, that is, a constant pointer, The content that the pointer points to is read only and cannot be modified. The TSTR represents TCHAR.

For how to choose which representation type to use, in addition to the developer's own habits, the recommendations that Windows gives are:

1 Use common data type TCHAR to represent text characters and strings;

2 uses explicit data types, such as Byte, to represent bytes, byte pointers, and data buffers;

3 Use _t macros to represent literal characters or strings.

Annoying various characters and character pointer types in Windows programming

Related Article

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.