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

here is a

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

Printable View

- 10-23-2007webznzcopying 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};`

- 10-23-2007King Mir
A loop.

Or a recursive function. Or use memcpy(). But a loop is best. - 10-23-2007webznz
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-2007webznz
okay.. heres my attempt.

Code:`for (i=0;i<4;i++){`

b[i] = a[i];

}

- 10-23-2007King Mir
Edit: It should be i<5, because there are 5 elements in the array.

- 10-23-2007webznz
dose the array not start from 0 tho? or am i thinking of somthing completly different there?

- 10-24-2007JFonseka
i<4 is correct

- 10-24-2007Elysia
It should be < 5 or <= 4, < 4 only copies the first 4 elements (0 - 3), while the array is (0 - 4).

- 10-24-2007dwks
In other words:

Code:`int from[N] = {...}, to[N], x;`

for(x = 0; x < N; x ++) {

to[x] = from[x];

}