Warning

You're browsing the old v2.x documentation. This version is no longer maintained. Click here to go the v3.x documentation.

STRNCPY

Syntax:

strncpy <string pointer destination>, <string pointer source>, <byte n>

The STRNCPY command is similar to STRCPY, the difference is that STRNCPY will copy the first n characters only. Copying will stop at the end of source even if n is greater than the length of source.

Example:

dim buffer![6]
a$ = "hello world"
b$ = @buffer!
strncpy b$, a$, 5
print b$
rem -- the above will output "hello"

STRNCPY can be used to emulate the CBM BASIC functions LEFT$ and MID$. The example above shows how to get the first n characters of a string. Here is an example for getting the middle of a string:

CBM BASIC code:

10 a$="hello world"
20 b$=mid$(a$, 4, 3)
30 print b$

XC=BASIC code:

a$ = "hello world"
dim buffer![6]
b$ = @buffer!
strncpy b$, a$+4, 3
print b$

Both programs will output “o w”.

Important note: The routine will not stop copying at the end of destination which may lead to an overflow situation. You have to make sure that there is enough memory at the destination.