10

I'm a little confused now with the hungarian notation prefixes in WinAPI for CHAR strings and WCHAR strings. When we use a CHAR string usually such a prefix is used:

CHAR szString[] = "Hello";

We have a zero-terminated string szString so everything's fine. But when we use a WCHAR string usually such a prefix is used:

WCHAR pwszString[] = L"Hello";

It stands for pointer to zero-terminated wide string... but our type doesn't look like this. Pointer to zero-terminated wide string is WCHAR** or PWSTR*. Am I wrong? Why it's sz for CHAR strings and pwsz but not wsz for WCHAR strings?

spandei
  • 219
  • 3
  • 16
  • 2
    Even the inventor of Hugarian notation said it was a bad idea. I would forget all about it. – john Apr 15 '13 at 14:33
  • Don't sweat it. A name is just a name. – Alexey Frunze Apr 15 '13 at 14:35
  • 3
    As with all Hungarian notation, the purpose of the `p` (applied to an array, but implying that it's a pointer) is to mislead and confuse future maintainers. The code was hard to write, so why should it be any easier to read? – Mike Seymour Apr 15 '13 at 14:43
  • 2
    @john: No. The inventor of Hungarian notation is horrified by people who blame him for "systems Hungarian", which is a bad idea. There's a lot to like about the way Simonyi used it. http://en.wikipedia.org/wiki/Hungarian_notation – Ben Voigt Apr 15 '13 at 18:37

2 Answers2

9

The second example is misleading (though not uncommon). It should be either one of these two:

WCHAR wszString[] = L"Hello";
WCHAR *pwszString = L"Hello";

Since an array can be used in most contexts that a pointer is expected, some programmers get a little sloppy about the distinction.

Hungarian is out of style, but it can be useful when used well.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • Oh, thanks, that was one of my guesses about wsz and pwsz:) And what prefix should have WCHAR** then? – spandei Apr 15 '13 at 20:48
  • `WCHAR **ppwszString`. Remember, that modern Hungarian is less about the underlying type that the compiler cares about, and more about the type of the data that the programmer needs to know about. The question really shouldn't be "What's the prefix for `WCHAR**`?". A `WCHAR**` might just be a pointer to a pointer to a single character, in that case, it would be `WCHAR **ppwch`. – Adrian McCarthy Apr 15 '13 at 23:46
-2

CHAR szString[] is synonymous with CHAR * szString. Both are pointers to strings. A pointer to a zero-terminated string is CHAR*, and a pointer to a zero-terminated wide string would be WCHAR* (not WCHAR**).

Now, if you want to get pedantic, you could (wrongly) claim that CHAR[] is "just an array of characters", in which case "rgc" might be the correct prefix.

RobH
  • 3,199
  • 1
  • 22
  • 27
  • 1
    They are only synonymous when appear as function parameters. Otherwise they are an array of CHAR and a pointer to CHAR respectively. – Alexey Frunze Apr 15 '13 at 14:37
  • Please enlighten me. What do you think the difference is between the two? – RobH Apr 15 '13 at 14:39
  • 4
    @RobH, for example, having `CHAR arrString[]` and `CHAR * pString`, you'll get different results for `sizeof(arrString)` and `sizeof(pString)`. – shakurov Apr 15 '13 at 14:44
  • 5
    @RobH: Arrays and pointers are as different as any two distinct types; there just happens to be a standard conversion from one to the other. As noted, they have different sizes; they also participate differently in overload resolution. – Mike Seymour Apr 15 '13 at 14:50
  • Interesting. At what point on the timeline from C to C++0x did arrays cease to be syntactic sugar for pointers? – RobH Apr 15 '13 at 14:53
  • @RobH: I'm not familiar with the history of C before 1989, but I'm fairly sure they've been distinct types, in both languages, forever. An array is a lump of storage containing objects; a pointer is the storage location of an object. – Mike Seymour Apr 15 '13 at 14:55
  • @Mike K&R section 5.3 may prove a useful reference and concurs with your comments. Selectively quoting (ahem), "[...]the name of an array is a synonym for the location of the initial element[...]". – RobH Apr 15 '13 at 15:07
  • @RobH Not in all contexts. What you're quoting is correct for the example you're taking the quote from. `sizeof` (already mentioned) and `&` produce different results on arrays. – Alexey Frunze Apr 15 '13 at 23:52
  • Thanks @AlexeyFrunze, Mike and shakurov. It's a good day when I learn something new (the return type of &array) and have to think about a fundamental language construct. – RobH Apr 16 '13 at 06:23
  • I've had such moments with C myself. I can relate to your experience. – Alexey Frunze Apr 16 '13 at 06:56