1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2006
Posts
1
Rep Power
0

#### Copy char array

Hi,
I'm trying to copy a char array to another. After that fill the array with char 0. For example if I have unsigned char a[20] and unsigned char b[64], then copy a to b and fill the rest of the array with char 0. This program must only works with array of char. Working with strings would be much easier, but with char array i donīt know how to do this. Thanks
2. Originally Posted by cheb
Hi,
I'm trying to copy a char array to another. After that fill the array with char 0. For example if I have unsigned char a[20] and unsigned char b[64], then copy a to b and fill the rest of the array with char 0. This program must only works with array of char. Working with strings would be much easier, but with char array i donīt know how to do this. Thanks
A standard for loop/assignment operation would wor If youre allowed, you could just use a memcpy/memset statement too. Sounds like a homework problem to me, therefore I'm not going to provide any code.
3. Here's some pseudo-code. src is the array that you are copying from, and dest is the array you are copying to

Code:
```for each character in src
dest = assoicated src character
for the rest of the characters
assoicated dest = 0```
It may look like a restatement of what you asked, but that should be it.

For the second loop you can calculcate the total number of iterations by subtracting the length of src from dest, then add to that the length of src for the assignment offsets into the array.
4. Probably easier (but less efficient) to set all elements of b to zero initially and then simply copy a to b.

There is also another way. HINT: One of the standard C functions does this already.

SCORPY's PUZZLE OF THE WEEK: PM me the function name and you'll appear on my sig for a week.

• mateoc15 agrees : Haha, I wish I had seen this just after the posting!
5. Originally Posted by Scorpions4ever
Probably easier (but less efficient) to set all elements of b to zero initially and then simply copy a to b.

There is also another way. HINT: One of the standard C functions does this already.

SCORPY's PUZZLE OF THE WEEK: PM me the function name and you'll appear on my sig for a week.
strncpy
Syntax:

#include <string.h>
char *strncpy( char *to, const char *from, size_t count );

The strncpy() function copies at most count characters of from to the string to. If from has less than count characters, the remainder is padded with '\0' characters. The return value is the resulting string.
Code:
`strncpy(&b[0], &a[0], 64);`

• kicken agrees
6. No Profile Picture
arj
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2005
Posts
71
Rep Power
10
Originally Posted by para45
Here's some pseudo-code. src is the array that you are copying from, and dest is the array you are copying to

Code:
```for each character in src
dest = assoicated src character
for the rest of the characters
assoicated dest = 0```

Here's exactly what it means:

#include<iostream>
using namespace std;

int main()
{
const int size1 = 3;//you can make
const int size2 = 6;//these dynamic given that size1<=size2
unsigned char src[size1];
unsigned char dest[size2]={'0'};

//initialize src array
for(int i=0; i<size1; i++)
cin>>src[i];

//Copy src to dest
for(int i=0; i<size1; i++)
dest[i] = src[i];

//OUTPUT dest:
for(int i=0; i<size2; i++)
cout<< dest[i]<<"\t";

return 0;
}