[英]error: implicit declaration of function ‘memcpy_s’; did you mean ‘memcpy’? [-Werror=implicit-function-declaration]
I am trying to make a memcpy_s() call.我正在尝试进行 memcpy_s() 调用。 Below is my code snippet where I am observing warning " error: implicit declaration of function 'memcpy_s'; did you mean 'memcpy'? [-Werror=implicit-function-declaration]"
下面是我的代码片段,我在其中观察到警告“错误:function 'memcpy_s'的隐式声明;你的意思是'memcpy'吗?[-Werror=implicit-function-declaration]”
#include <stdio.h>
#include <string.h>
#include <stdint.h>
static const uint32_t ab[10] = {0,1,2,3,4,5,6,7,8,9};
void main()
{
uint32_t u[10];
memcpy_s((void *)&u[0], sizeof(u), (void *)&ab[0], 10 * sizeof(uint32_t));
}
It appears that the IDE/Platform you are using doesn't support the memcpy_s()
variant of memcpy
, which IMO is good!您使用的 IDE/平台似乎不支持
memcpy
的memcpy_s()
变体,IMO 很好! I'd recommend using memcpy()
instead.我建议改用
memcpy()
。
If you haven't already used it before --如果您之前没有使用过它——
Looking at the reference page for memcpy
查看
memcpy
的参考页面
void *memcpy(void *restrict dest, const void *restrict src, size_t n);
void *memcpy(void *restrict dest, const void *restrict src, size_t n);
It needs the following arguments:它需要以下 arguments:
dest - pointer to the object to copy to
dest - 指向要复制到的 object 的指针
src - pointer to the object to copy fromsrc - 指向要从中复制的 object 的指针
n - number of bytes to copyn - 要复制的字节数
So,所以,
memcpy((void*)&u[0], (void*)&ab[0], 10*sizeof(uint32_t));
Or better,或更好,
memcpy((void*)&u[0], (void*)&ab[0], COUNT_OF(ab)*sizeof(uint32_t));
// ↑ Note this!
with a minimalistic, not-so-perfect macro to get count of elements in the array:使用简约的、不太完美的宏来获取数组中元素的数量:
#define COUNT_OF(var) (sizeof(var)/sizeof(var[0]))
By the way, there is a typo in your code:顺便说一句,您的代码中有一个错字:
memcpy_s(void *)&u[0], sizeof(u), (void*)&ab[0], 10*sizeof(uint32_t));
// ↑ ↑ ouch!
is the parenthesis.是括号。 If you at all migrate to a platform where you have
memcpy_s()
supported then change it to:如果您完全迁移到支持
memcpy_s()
的平台,请将其更改为:
memcpy_s((void*)&u[0], sizeof(u), (void*)&ab[0], COUNT_OF(ab)*sizeof(uint32_t));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.