C++ Why const LPSTR is different than const char *?

sooqua

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*?

Angew is no longer proud of SO

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ Why const LPSTR is different than const char *?

From Dev

Why const char* implicitly converted to bool rather than std::string?

From Dev

Why const char* implicitly converted to bool rather than std::string?

From Dev

Modify const char * in C

From Dev

Redefine const char* in C

From Dev

c++: why can't we convert char ** to const char **

From Java

Why can const char* const & = "hello" compile?

From Dev

const int and const char* (Why use a pointer?)

From Dev

Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

From Dev

C: Assigning "static const char * const" to "static const char *"

From Dev

C: Assigning "static const char * const" to "static const char *"

From Dev

c++ const char* to char*

From Dev

C convert const char * to char

From Dev

(const char*)++ or (const char)++?

From Dev

Casting a const void * to a const char * in C

From Dev

Why conversion from char* to std::string is more preferred than to const char* in the snippet?

From Dev

C++11 introduced exception constructors taking `const char*`. But why?

From Java

Why is there now a difference between "{static const char a[]={...}" and "{const char a[]={...}"?

From Dev

Why const char * and const char [] as function parameter are considered to be equivalent?

From Dev

Why does ostream::write() require ‘const char_type*’ instead of ‘const void*’ in C++?

From Dev

Returning strings with const char * in C

From Dev

C modifying register const char *

From Dev

Passing const char * to a function in C

From Dev

C Preprocessor: dynamic const char

From Dev

Passing const char * to a function in C

From Dev

C++ arrays const char

From Dev

DllImport C Sharp const char*

From Dev

const char* vs const char[]

From Java

How to convert const char* to char* in C?

Related Related

  1. 1

    C++ Why const LPSTR is different than const char *?

  2. 2

    Why const char* implicitly converted to bool rather than std::string?

  3. 3

    Why const char* implicitly converted to bool rather than std::string?

  4. 4

    Modify const char * in C

  5. 5

    Redefine const char* in C

  6. 6

    c++: why can't we convert char ** to const char **

  7. 7

    Why can const char* const & = "hello" compile?

  8. 8

    const int and const char* (Why use a pointer?)

  9. 9

    Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

  10. 10

    C: Assigning "static const char * const" to "static const char *"

  11. 11

    C: Assigning "static const char * const" to "static const char *"

  12. 12

    c++ const char* to char*

  13. 13

    C convert const char * to char

  14. 14

    (const char*)++ or (const char)++?

  15. 15

    Casting a const void * to a const char * in C

  16. 16

    Why conversion from char* to std::string is more preferred than to const char* in the snippet?

  17. 17

    C++11 introduced exception constructors taking `const char*`. But why?

  18. 18

    Why is there now a difference between "{static const char a[]={...}" and "{const char a[]={...}"?

  19. 19

    Why const char * and const char [] as function parameter are considered to be equivalent?

  20. 20

    Why does ostream::write() require ‘const char_type*’ instead of ‘const void*’ in C++?

  21. 21

    Returning strings with const char * in C

  22. 22

    C modifying register const char *

  23. 23

    Passing const char * to a function in C

  24. 24

    C Preprocessor: dynamic const char

  25. 25

    Passing const char * to a function in C

  26. 26

    C++ arrays const char

  27. 27

    DllImport C Sharp const char*

  28. 28

    const char* vs const char[]

  29. 29

    How to convert const char* to char* in C?

HotTag

Archive