问题描述
我最近在我的项目中找到了调用
我在VS2005,VS2008和VS2010中验证了这个行为。
I verified this behavior in VS2005, VS2008 and VS2010.
推荐答案
& a 是数组的地址; a 可以隐式转换为第一个元素的地址。两者都有相同的地址,因此当转换为 void * 时,两者将给出相同的值。
&a is the address of the array; a can be implicitly converted to the address of the first element. Both have the same address, and so both will give the same value when converted to void*.
memcpy ,所以它很容易编写代码编译,但行为不好;例如,如果 a 是一个指针而不是一个数组,那么 sizeof a 将会编译但给出错误的值。 C ++的类型安全模板可以防止:
memcpy is not typesafe, so it's quite easy to write code that compiles but behaves badly; for example, if a were a pointer rather than an array, then sizeof a would compile but give the wrong value. C++'s typesafe templates can protect against that:
std::copy(b, std::end(b), a); // will only compile if `b` has a known end.
这篇关于将memcpy与数组名称一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!