Skip to content

Latest commit

 

History

History
67 lines (63 loc) · 1.58 KB

File metadata and controls

67 lines (63 loc) · 1.58 KB
title Character Assignment | Microsoft Docs
ms.custom
ms.date 11/04/2016
ms.reviewer
ms.suite
ms.technology
cpp-windows
ms.tgt_pltfrm
ms.topic article
dev_langs
C++
helpviewer_keywords
characters [C++], assignments
MBCS [C++], character assignments
ms.assetid dcc329cd-92df-4e20-817d-364be62ff28f
caps.latest.revision 9
author ghogen
ms.author ghogen
manager ghogen
translation.priority.ht
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

Character Assignment

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 copies 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_s( sz1, 1, sz2 );  
        sz1 = _mbsinc( sz1 );  
        sz2 = _mbsinc( sz2 );  
    }  
    else  
        sz2 = _mbsinc( sz2 );  
}  

See Also

MBCS Programming Tips
Character Comparison