LPSTR
Type Definition
typedef char *LPSTR;
The LPSTR type is a pointer to a null-terminated string of 8-bit characters (ANSI characters).
In the Windows API, strings can be represented in several ways:
- ANSI Strings: These are strings of single-byte characters, where each character is represented by an 8-bit value. The character set used is typically the system's default ANSI code page. LPSTR is used for pointers to these strings.
- Unicode Strings: These are strings of 16-bit characters, where each character is represented by a 16-bit value. The most common Unicode encoding on Windows is UTF-16. LPWSTR is used for pointers to these strings.
LPSTR is an alias for char *. When you see a function parameter or return type defined as LPSTR, it indicates that the function expects or returns a pointer to a character array that is terminated by a null character ('\0').
Usage
LPSTR is commonly used in older Windows APIs or when explicit ANSI string handling is required. Most modern Windows development is encouraged to use Unicode (LPWSTR) for better internationalization support.
Example:
#include <windows.h>
#include <iostream>
int main() {
LPSTR ansiString = "Hello, Windows API!"; // A null-terminated ANSI string literal
// Example of passing an LPSTR to a hypothetical function
// void DisplayAnsiString(LPSTR text);
// DisplayAnsiString(ansiString);
std::cout << "String content: " << ansiString << std::endl;
// Note: Modifying strings pointed to by string literals can lead to undefined behavior.
// For modifiable strings, allocate memory:
char buffer[100];
strcpy_s(buffer, sizeof(buffer), "Another string");
LPSTR modifiableString = buffer;
std::cout << "Modifiable string: " << modifiableString << std::endl;
return 0;
}
Related Types
- LPWSTR: Pointer to a null-terminated string of 16-bit characters (Unicode).
- LPCSTR: Pointer to a constant null-terminated string of 8-bit characters (ANSI).
- LPCWSTR: Pointer to a constant null-terminated string of 16-bit characters (Unicode).