Share via


Character Assignment

Unicode TasksMultibyte Character Set (MBCS) Tasks

Consider the following example, in which the while loop scans a string, copying all characters except ‘X’ into another string:

while( *sz2 )
{
    if( *sz2 != 'X' )
        *sz1++ = *sz2;
    else
        sz2++;
}

The code copies the byte at sz2 to the location pointed to by sz1, then increments sz1 to receive the next byte. But if the next character in sz2 is a double-byte character, the assignment to sz1 will copy only the first byte. The following code uses a portable function to copy the character safely and another to increment sz1 and sz2 correctly:

while( *sz2 )
{
    if( *sz2 != 'X' )
    {
        _mbscpy( sz1, sz2 );
        sz1 = _mbsinc( sz1 );
    }
    else
        sz2 = _mbsinc( sz2 );
}

See Also   Character Comparison