John Crouch wrote:
> Char dst[10];
> StrNCopy(dst, "Longer than 10 bytes", 10);
> dst[9] = 0;

As John Marshall said, check the docs. This code won't work with multi-byte
characters because the last statement might be zeroing the second byte of a
two-byte character. If you want your code to work with multi-byte
characters, don't let StrNCopy see the entire buffer:

  Char dst[10];
  StrNCopy(dst, "Longer than 10 bytes", 9); // save the last byte
  dst[9] = 0;

The last two lines could go in either order; they operate on adjacent, but
non-overlapping areas in memory. This works because of how StrNCopy deals
with clipping. If a two-byte character doesn't quite fit, the "extra" byte
is zeroed. 

James wrote:
> Another method is to initialize dst[0] = '\0' and then use StrNCat instead
> of StrNCopy.

That's what I do.
--
Danny @ PalmSource

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to