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:

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

See Also