Board index » delphi » how to copy part of char array to another char array

how to copy part of char array to another char array

Hi all,

I think this must be a simpel question, but really at a loss.

If I have 2 char array and want to copy the last part of the first into the
second, how do I do that?

example:

char String1[10]
char String2[6]

strcpy(String1, "0123456789")
and now I want String2 to be "456789"

TIA,

Leif

 

Re:how to copy part of char array to another char array


strcpy (String2, &String1[4]);

Dominic

Quote
"Leif Rasmussen" <l...@spinformatik.dk> wrote in message

news:3a2650e5_2@dnews...
Quote
> Hi all,

> I think this must be a simpel question, but really at a loss.

> If I have 2 char array and want to copy the last part of the first into
the
> second, how do I do that?

> example:

> char String1[10]
> char String2[6]

> strcpy(String1, "0123456789")
> and now I want String2 to be "456789"

> TIA,

> Leif

Re:how to copy part of char array to another char array


On Thu, 30 Nov 2000 14:05:27 +0100, "Leif Rasmussen" <l...@spinformatik.dk>
wrote:

Quote
>char String1[10]
>char String2[6]
>strcpy(String1, "0123456789")

This will result in a data overrun, as strcpy() will copy a terminating nul
character to the memory location following the last location of the array
String1. Similarly, String2 is not large enough to hold the desired value:

Quote
>I want String2 to be "456789"

Since you're using the data as C-style strings, rather than as simple arrays
of characters, you must allow room for a terminating nul character. Both
String1 and String2 should be defined one character larger than the
data to be held.

To copy a part of the source string, simply use strcpy() with an appropriate
index into the source string, or with a pointer plus increment.

--
Wayne A. King
(ba...@torfree.net, wayne.k...@ablelink.org,
 wak...@idirect.com, Wayne_A_K...@compuserve.com)

Re:how to copy part of char array to another char array


On Thu, 30 Nov 2000 06:21:13 -0700, "Dominic I. Holmes"

Quote
<DIH.J...@RemoveForNonSpam.DigitalFormation.com> wrote:
>strcpy (String2, &String1[4]);

This will result in a data overrun with the array sizes used in the example.
There is no room for a terminating nul in either array.

Quote
>>char String1[10]
>>char String2[6]
>>I want String2 to be "456789"

--
Wayne A. King
(ba...@torfree.net, wayne.k...@ablelink.org,
 wak...@idirect.com, Wayne_A_K...@compuserve.com)

Re:how to copy part of char array to another char array


Leif,

strcpy( String2, String1+4 );

Andrue Cope
[Bicester, UK]

Re:how to copy part of char array to another char array


Try memcpy.

Quote
"Leif Rasmussen" <l...@spinformatik.dk> wrote in message

news:3a2650e5_2@dnews...
Quote
> Hi all,

> I think this must be a simpel question, but really at a loss.

> If I have 2 char array and want to copy the last part of the first into
the
> second, how do I do that?

> example:

> char String1[10]
> char String2[6]

> strcpy(String1, "0123456789")
> and now I want String2 to be "456789"

> TIA,

> Leif

Other Threads