|
Hello thar,
I'm learning to program Windows(still) and it's going ok - finally got the grasp on UNICODE(I think!). But I'm wondering a bit about functions that accept a pointer as an argument, CreateWindow for instance:
hwnd = CreateWindow (LPCTSTR lpClassName, LPCTSTR lpWindowName, ...);
As far as I've understood, LPCTSTR is a long pointer to a string, so I'd expect to pass it something like this:
TCHAR hi[] = TEXT("hello"); hwnd = CreateWindow(&hi, &hi, ...);
Or a pointer to that variable. But instead I can just pass the TCHAR variable and that's what works. What am I missing here?
Thanks in advance for any help!
|
As far as i remember LPCTSTR isn't a pointer but an actual String, so your definition of the variable hi is exactly the correct data-type(sorry i only know this expression in german but i think you'll get what i mean).
|
LPCTSTR = Long Pointer to a Const TCHAR STRing ^^
|
Germany2896 Posts
I think c++ has an implicit convert from an array of char(I think that's the type of a string literal) to a PChar
|
|
Germany2896 Posts
Stroustrup on string literals:
The type of a string literal is ‘‘array of the appropriate number of const characters,’’ so "Bohr" is of type "const char [5]". A string literal can be assigned to a char *. This is allowed because in previous definitions of C and C++ , the type of a string literal was char *. Allowing the assignment of a string literal to a char* ensures that millions of lines of C and C++ remain valid. It is, however, an error to try to modify a string literal through such a pointer
|
Aha, so that's how it is Thanks a lot for the help!
On September 17 2008 03:39 HeavOnEarth wrote: lmaooo xD
Happy to entertain I guess ...?
|
oh i was just like "lmao ( I DONT GET ANYTHING OF THIS) xD " of course i didn't feel it necessary to post the parenthesis' part at the time!
|
it's one of those C miseries that survived through the ages. You shouldn't really base your code on something like this but it's quite neat to know and it's one of the major causes of errors in code.
|
TCHAR hi[] = TEXT("hello");
&hi is a pointer to an array of TCHARs, which is not a pointer to TCHARs, which is expected by CreateWindow. Instead, you would use hi, as it's an array, which is implicitely converted to TCHAR *. Or you would use &hi[0] to explicitely state what you really want..
btw, an advice: use const for everything that are not going to change (hi).
|
On September 17 2008 13:35 HeavOnEarth wrote: oh i was just like "lmao ( I DONT GET ANYTHING OF THIS) xD " of course i didn't feel it necessary to post the parenthesis' part at the time!
Yeah, then the post might have some relevance to it. God forbid
|
|
|
|