下面的程序没有输出想要的数据(在VC2008编译器上)
#include <stdio.h>
#include <string.h>
int main(void)
{
int i;
int dest[10] = {1};
int src [] = {2, 3, 4, 5, 6};
memcpy(dest, src, 5);
for (i=0; i<10; i++) printf("%i\n", dest[i]);
return 0;
}
而使用 char
数组代替,一切正常!
问题出在哪里?
最佳答案
memcpy 需要一定数量的字节来复制——而不是一定数量的对象。
memcpy(dest,src,5*sizeof(dest[0]))
关于c - 使用非字符数据作为参数的 memcpy 中的问题!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3729929/