# copying values of an array to another array?!

• 10-23-2007
webznz
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?...
• 10-23-2007
King Mir
A loop.

Or a recursive function. Or use memcpy(). But a loop is best.
• 10-23-2007
webznz
of course it is! jeppers! im starting to worrie about my exams! lol!!!! i have the worst memory! lol! thanks for your help
• 10-23-2007
webznz
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.
• 10-23-2007
King Mir
Edit: It should be i<5, because there are 5 elements in the array.
• 10-23-2007
webznz
dose the array not start from 0 tho? or am i thinking of somthing completly different there?
• 10-24-2007
JFonseka
i<4 is correct
• 10-24-2007
Elysia
It should be < 5 or <= 4, < 4 only copies the first 4 elements (0 - 3), while the array is (0 - 4).
• 10-24-2007
dwks
In other words:
Code:

```int from[N] = {...}, to[N], x; for(x = 0; x < N; x ++) {     to[x] = from[x]; }```