memcpy()函数

定义

void * memcpy ( void * destination, const void * source, size_t num );

将source指向的地址处的 num 个字节 拷贝到 destination 指向的地址处。注意,是字节。

题目

int main() {
	int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
	memcpy(a + 3, a, 5);
	for (int i = 0; i<10; i++){
		printf("%d ", a[i]);
	}
	return 0;
}

解答:0 1 2 0 1 5 6 7 8 9

因为memcpy的最后一个参数是需要拷贝的字节的数目!一个int类型占据4个字节!这样的话,本题5字节,实际上只能移动2个数字(往大的去)。如果要想达到将a地址开始的5个元素拷贝到a+3地址处,需要这么写:

memcpy(a + 3, a, 5*sizeof(int));

参考

https://blog.csdn.net/baidu_35679960/article/details/80953973

https://my.oschina.net/zidanzzg/blog/812887

发布了166 篇原创文章 · 获赞 48 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_38769551/article/details/105388573
今日推荐