We all know how to find the length of a string. For example, if we have defined a pointer to a character string like so:
char * pc = "Hello!" ;
we can call
iLength = strlen (pc) ;
The variable iLength will be set equal to 6, the number of characters in the string.
Excellent! Now let's try defining a pointer to a string of wide characters:
wchar_t * pw = L"Hello!" ;
And now we call strlen again:
iLength = strlen (pw) ;
Now the troubles begin. First, the C compiler gives you a warning message, probably something along the lines of
`function' : incompatible types - from `unsigned short *' to `const char *'
It's telling you that the strlen function is declared as accepting a pointer to a char, and it's getting a pointer to an unsigned short. You can still compile and run the program, but you'll find that iLength is set to 1. What happened?
The 6 characters of the character string "Hello!" have the 16-bit values:
0x0048 0x0065 0x006C 0x006C 0x006F 0x0021
which are stored in memory by Intel processors like so:
48 00 65 00 6C 00 6C 00 6F 00 21 00
The strlen function, assuming that it's attempting to find the length of a string of characters, counts the first byte as a character but then assumes that the second byte is a zero byte denoting the end of the string.
本文探讨了在C语言中使用strlen函数计算宽字符字符串长度时遇到的问题,并详细解释了为何会出现错误的结果。通过分析内存中宽字符的存储方式,揭示了strlen函数的工作原理及其在处理宽字符时的局限性。

被折叠的 条评论
为什么被折叠?



