Discuss Copy char array in the C Programming forum on Dev Shed. Copy char array C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 1
Time spent in forums: 1 h 39 m 53 sec
Reputation 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
Posts: 5,654
Time spent in forums: 2 Months 2 Weeks 2 Days 5 h 1 m 44 sec
Reputation Power: 3436
Quote:
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.
Posts: 264
Time spent in forums: 6 Days 2 h 28 m 19 sec
Reputation Power: 123
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.
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 30 m 25 sec
Reputation Power: 4080
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.
__________________ Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
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.
Posts: 71
Time spent in forums: 1 Day 2 h 48 m 28 sec
Reputation Power: 8
Quote:
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'};