Thread: copying values of an array to another array?!

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    51

    copying values of an array to another array?!

    Okay say i have a array a of 5 integers how do i copy the values to an array b?

    here is a

    Code:
    int a[5] = {5,4,3,2,1};
    what code could i write to copy that into another array b?...

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    A loop.

    Or a recursive function. Or use memcpy(). But a loop is best.
    Last edited by King Mir; 10-23-2007 at 02:50 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    of course it is! jeppers! im starting to worrie about my exams! lol!!!! i have the worst memory! lol! thanks for your help

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    okay.. heres my attempt.

    Code:
    for (i=0;i<4;i++){
      b[i] = a[i];
    }
    im not sure about the b[i]=a[i] would that work? or do i need another variable for the array in a? any suggestions would be appreciated.
    Last edited by webznz; 10-23-2007 at 03:07 PM. Reason: mistake

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Edit: It should be i<5, because there are 5 elements in the array.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    dose the array not start from 0 tho? or am i thinking of somthing completly different there?

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    i<4 is correct

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It should be < 5 or <= 4, < 4 only copies the first 4 elements (0 - 3), while the array is (0 - 4).

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In other words:
    Code:
    int from[N] = {...}, to[N], x;
    
    for(x = 0; x < N; x ++) {
        to[x] = from[x];
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM
  2. Count distinct array values
    By rkooij in forum C Programming
    Replies: 4
    Last Post: 10-03-2006, 03:03 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. 3-d array assign string values
    By WaterNut in forum C++ Programming
    Replies: 8
    Last Post: 07-01-2004, 12:02 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM