Thread: Copy char array

    #1
  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. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,953
    Rep Power
    4033
    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.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    264
    Rep Power
    124
    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.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    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.

    Comments on this post

    • mateoc15 agrees : Haha, I wish I had seen this just after the posting!
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Super User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Posts
    648
    Rep Power
    75
    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);

    Comments on this post

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

    Join Date
    Dec 2005
    Posts
    71
    Rep Power
    9
    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;
    }

    Comments on this post

    • jafet disagrees : Don't do his homework for him!

IMN logo majestic logo threadwatch logo seochat tools logo