sz and pwsz prefixes in WinAPI
Asked Answered
D

2

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?

Dressel answered 15/4, 2013 at 14:29 Comment(4)
Even the inventor of Hugarian notation said it was a bad idea. I would forget all about it.Diadem
Don't sweat it. A name is just a name.Tecu
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?Casia
@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. en.wikipedia.org/wiki/Hungarian_notationSlinky
R
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.

Rutherfordium answered 15/4, 2013 at 18:31 Comment(2)
Oh, thanks, that was one of my guesses about wsz and pwsz:) And what prefix should have WCHAR** then?Dressel
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.Rutherfordium
D
-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.

Deenadeenya answered 15/4, 2013 at 14:35 Comment(10)
They are only synonymous when appear as function parameters. Otherwise they are an array of CHAR and a pointer to CHAR respectively.Tecu
Please enlighten me. What do you think the difference is between the two?Deenadeenya
@RobH, for example, having CHAR arrString[] and CHAR * pString, you'll get different results for sizeof(arrString) and sizeof(pString).Nimrod
@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.Casia
Interesting. At what point on the timeline from C to C++0x did arrays cease to be syntactic sugar for pointers?Deenadeenya
@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.Casia
@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[...]".Deenadeenya
@Deenadeenya 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.Tecu
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.Deenadeenya
I've had such moments with C myself. I can relate to your experience.Tecu

© 2022 - 2024 — McMap. All rights reserved.