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.