Optimal way to perform a shift operation on an array

前端 未结 6 1464
醉酒成梦
醉酒成梦 2021-02-20 08:00

Suppose I have an array

unsigned char arr[]= {0,1,2,3,4,5,6,7,8,9};

Is there a way to perform shift operation on them besides just copying them

6条回答
  •  灰色年华
    2021-02-20 08:42

    As long as the array is modifiable, you can use memmove to shift them (but don't mistakenly use memcpy as memcpy is not meant for overlapping regions):

    memmove(&arr[0], &arr[1], sizeof(arr) - sizeof(*arr));
    

    (sizeof(arr) - sizeof(*arr) is the size in bytes of all but 1 element of the array).

提交回复
热议问题