定义一个指针字符串:
char *pc = "Hello!";
iLength = strlen(pc);
变量iLength=6即字串中的字元数
那么定义一个指向宽字元的指针
wchar_t *pw = L"Hello!";
在调用 strlen 则出现错误了
'function': incompatible types - 'unsigned short *'to "const char*"
这条消息是说 宣告strlen函数是,该函数应接受char类型的指针,但他却接收了unsigned short 类型的指针。
仍然可以编译并执行改程序,但是iLength = 1; why
字串"Hello!" 6个子元占16个位元
0x0048 0x0065 0x006c 0x006c 0x006F 0x0021
Intel处理器在记忆体中将其存为:
48 00 65 00 6C 00 6C 00 6F 00 21 00
假定strlen函式正试图得到一个字串的长度,并把第1个位元组作为字元开始计数,但接著假定如果下一个位元组是0,则表示字串结束。
strlen函式的宽字元版是wcslen(wide-character string length:宽字串长度),并且在STRING.H(其中也说明了strlen)和WCHAR.H中均有说明。strlen函式说明如下:
size_t __cdecl strlen (const char *) ;而wcslen函式则说明如下:
size_t __cdecl wcslen (const wchar_t *) ;这时我们知道,要得到宽字串的长度可以呼叫
iLength = wcslen (pw) ;