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};
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};
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.
of course it is! jeppers! im starting to worrie about my exams! lol!!!! i have the worst memory! lol! thanks for your help
okay.. heres my attempt.
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.Code:for (i=0;i<4;i++){ b[i] = a[i]; }
Last edited by webznz; 10-23-2007 at 03:07 PM. Reason: mistake
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.
dose the array not start from 0 tho? or am i thinking of somthing completly different there?
i<4 is correct
It should be < 5 or <= 4, < 4 only copies the first 4 elements (0 - 3), while the array is (0 - 4).
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.