Hope my question is clear and relavent, new to Pointers… – Can I copy a whole portion of an array at once, by refering to the pointer to the location of the first slot in the array I want to begin copying from?
For example –
Given an array : A [ 1,2,3,4,5,7,8,3,2,5,1,0,9]
– I want to copy only the part of the array from the n’th slot on, into the beginning of the array B [0 0 0 ….. ] (B is of the same length of A).
Can I do it at once, using pointers instead of a loop? Something like – switching the pointer to the 1’st slot in B with the pointer to the n’th slot of A, and the n’th slot in B with the last one in A?
Thanks a lot on advance!
That’s what
memcpyis for.where
Nis the number of elements inA. IfAis really an array (not just a pointer to one), thenNcan be computed assizeof(A) / sizeof(A[0]), so the call simplifies tomemcpylives in<string.h>; its first argument is the destination of the copy, its second the source.(I’m sorry, I don’t really follow what kind of pointer trick you have in mind, so I can’t comment on that.)