We all know how to get the length of a string. For example, if we have defined a string pointer as follows:
Char * Pc = "Hello! ";
We can call
Ilength = strlen (PC );
At this time, the variable ilength is equal to 6, that is, the number of characters in the string.
Great! Now let's try to define a pointer to a wide character:
Wchar_t * PW = l "Hello! ";
Call strlen again:
Ilength = strlen (PW );
Now we are in trouble. First, the C compiler will display a warning message, which may be:
'Function': incompatible types-from 'unsigned short * 'to 'const char *'
This message indicates that when the strlen function is declared, the function should receive the char type indicator, but it now receives an unsigned short type indicator. You can still compile and execute thisProgramBut you will find that ilength is equal to 1. Why?
String "Hello !」 The six characters in the string take up 16 characters:
0x0048 0x0065 0x006c 0x006c 0x006f 0x0021
The intel processor saves the following in memory:
48 00 65 00 6C 00 6C 00 6f 00 21 00
Assume that the strlen function is trying to get the length of a string and count 1st bytes as characters. If the next byte is 0, the string ends.
This small exercise clearly illustrates the differences between the C language itself and the linked library functions during execution. The compiler splits the string l "Hello! "Is interpreted as a group of 16-bit short integer data and saved in the wchar_t array. The compiler also processes array indexes and sizeof operators, so these operations can work normally, but the execution period linked library function, such as strlen, is added only when linking. These functions assume that a string consists of single-byte characters. When a wide string is encountered, the function is not executed as we expected.
You may say, "Oh, it's too much trouble !」 Currently, each c-language linked library function must be rewritten to accept wide characters. But in fact, not every c-language linked library function needs to be rewritten, but those functions with string parameters need to be rewritten, and you do not need to complete it. They have been overwritten.
The wide character version of The strlen function is wcslen (wide-Character String Length: width string length), which is described in string. H (strlen) and wchar. h. Strlen functions are described as follows:
Size_t _ cdecl strlen (const char *);
The wcslen function is described as follows:
Size_t _ cdecl wcslen (const wchar_t *);
At this time, we know that to obtain the length of a wide string, you can call
Ilength = wcslen (PW );
The function returns 6 Characters in the string. Remember, the character length of the character string is not changed after the character segment is changed to a wide character segment, but the length of the bit group is changed.
All the C execution period linked library functions you are familiar with have wide character versions. For example, wprintf is a wide character version of printf. These functions are described in wchar. h and in the header file containing the standard function description.