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
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.
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];
}
Edit: It should be i<5, because there are 5 elements in the array.
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];
}