Why does the following code compiles?
void foo(const LPSTR str) {
str[0] = '\0';
}
while
void foo(LPCSTR str) {
str[0] = '\0';
}
and
void foo(const char* str) {
str[0] = '\0';
}
does not.
It's actually LPTSTR
in my code so const
version is LPCTSTR
... So can I increase code readability by having something like const LPTSTR
, or it must be either LPCTSTR
or const TCHAR*
?
A typedef "seals" the type from outside modification. LPSTR
is a char *
, period. Adding const
to that (as const LPSTR
) adds the const
to the outside: you get a char * const
.
What you want is to "inject" the const
(apply it the pointee, not to the pointer), and that is not possible through a simple declaration syntax with a typedef. So it must be LPCSTR
, the typedef created for exactly this purpose.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments