Thread: copy array to another array

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    13

    copy array to another array

    how do you copy one array to another?
    Code:
    array1 = array2;
    that code doesn't work, does it?

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    No. If you know the size of the array:
    Code:
    #define ARRAY_SIZE 3
    int array1[ARRAY_SIZE] = {};
    int array2[ARRAY_SIZE] = {1,3,4};
    for (int i = 0; i < ARRAY_SIZE; i++)
    {
         array1[i] = array2[i];
    }
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    memcpy? There are a few more, but they all pretty much do what's been shown above.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Code:
    struct S
    {
        char arr[3];
    };
    
    int main( int argc, char *argv[] )
    {
      struct S s1 = { {'1','2','3'} };
      struct S s2 = { {'4','5','6'} };
    
      s1 = s2;
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making a copy of an array by copying its address..
    By transgalactic2 in forum C Programming
    Replies: 12
    Last Post: 04-20-2009, 11:07 AM
  2. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 01:51 PM
  3. Copy multidimensional array to single array?
    By seepox in forum C Programming
    Replies: 9
    Last Post: 05-08-2006, 11:19 AM
  4. copy contents of array to single value?
    By mapunk in forum C Programming
    Replies: 3
    Last Post: 12-02-2005, 09:28 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM