2.2.3.15.11 string::GetBuffer

Description

Retrieve a pointer to the internal character buffer for the string.

Syntax

char * GetBuffer( int nMinBufLength )

Parameters

nMinBufLength
[input]Specifies the minimum size of the character buffer in characters.
This value does not include space for a null terminator.
To get the internal buffer without any reallocation, use 0

Return

This is an LPTSTR pointer to the character buffer of the null-terminated object.

Examples

EX1

void string_GetBuffer_ex1()
{
    string str = "abcdefg";
    char *p = str.GetBuffer( 20 ); 
    p[3] = 'D';  
    str.ReleaseBuffer();
    out_str( str );        //output should be "abcDefg"
}

EX2

void string_GetBuffer_ex1()
{
    string str = "abcdefg";
    char *p = str.GetBuffer(0); //no need to specify if we know we are not extending it
    p[3] = 'D';  
    str.ReleaseBuffer();
    out_str( str );        //output should be "abcDefg"
}

Remark

If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other string methods.

It is preferable to use GetBuffer(0) instead of GetBuffer(str.GetLength()).

See Also

string::ReleaseBuffer

Header to Include

origin.h