I am developing in Cocoa / Xcode and have an int * array containing values.
When I want to use memcpy to shift values in the array, it transfers only 0s.
e.g.
- Array contains values as 1 2 3 4
memcpy(array,array+2*sizeof(int),2*sizeof(int));
result: 0,0,3,4
Is there a better alternative to memcpy, or something I am doing wrong ?
I think the right way to do what you want is:
That’s because in the 2nd argument of memcpy pointer arithmetic is being done, and anything that’s being added to “array” pointer is considered as being a multiple of an integer’s size. So in this case saying “array + 2” means “the integer pointer `array’ plus two times the size of an integer”.