| title | Incrementing and Decrementing Pointers | Microsoft Docs | ||||
|---|---|---|---|---|---|
| ms.custom | |||||
| ms.date | 11/04/2016 | ||||
| ms.reviewer | |||||
| ms.suite | |||||
| ms.technology |
|
||||
| ms.tgt_pltfrm | |||||
| ms.topic | article | ||||
| dev_langs |
|
||||
| helpviewer_keywords |
|
||||
| ms.assetid | 0872b4a0-e2bd-4004-8319-070efb76f2fd | ||||
| caps.latest.revision | 7 | ||||
| author | ghogen | ||||
| ms.author | ghogen | ||||
| manager | ghogen |
Use the following tips:
-
Point to lead bytes, not trail bytes. It is usually unsafe to have a pointer to a trail byte. It is usually safer to scan a string forward rather than in reverse.
-
There are pointer increment/decrement functions and macros available that move over a whole character:
sz1++;becomes:
sz1 = _mbsinc( sz1 );The
_mbsincand_mbsdecfunctions correctly increment and decrement incharacterunits, regardless of the character size. -
For decrements, you need a pointer to the head of the string, as in the following:
sz2--;becomes:
sz2 = _mbsdec( sz2Head, sz2 );Alternatively, your head pointer could be to a valid character in the string, such that:
sz2Head < sz2You must have a pointer to a known valid lead byte.
-
You might want to maintain a pointer to the previous character for faster calls to
_mbsdec.