memcpy() 移动结构数组的内容

memcpy() to move the contents of an array of structures

我正在尝试移动结构数组的内容。


#include<stdio.h>
#include<string.h>
typedef struct {
      char texts[1024];
}text_i;
text_i textlist[5];
int main()
{
int ind;
strcpy( textlist[0].texts, "hello .."); 
strcpy( textlist[1].texts, "world .."); 
printf("texts before memcpy\n");
for (ind = 0; ind < 5 ; ind++)
        printf("texts ind(%d) is %s \n", ind, textlist[ind].texts);
memcpy( &textlist[1], &textlist[0], 4 * sizeof(text_i));
printf("texts after memcpy\n");
for (ind = 0; ind < 5 ; ind++)
        printf("texts ind(%d) is %s \n", ind, textlist[ind].texts);
}


这会将 5 texts 的整个列表打印到字符串 hello ...


texts before memcpy
texts ind(0) is hello ..
texts ind(1) is world ..
texts ind(2) is
texts ind(3) is
texts ind(4) is
texts after memcpy
texts ind(0) is hello ..
texts ind(1) is hello ..
texts ind(2) is hello ..
texts ind(3) is hello ..
texts ind(4) is hello ..


我的意图是将 textlist[0] 移动到 textlist[1] ,将 textlist[1] 移动到 textlist[2] ,将 textlist[2] 移动到 textlist[3] 等等。

预计:

texts ind(0) is hello ..
texts ind(1) is hello ..
texts ind(2) is world ..
texts ind(3) is 
texts ind(4) is 

我不希望 ind(3)ind(4) 不被触及。怎么弄成上面的格式?

改用memcpy() to copy between overwrapped regions invokes undefined behavior. Use memmove()