阵列I具有简单的结构:如何使用memcpy初始化以struct
typedef struct{
double par[4];
}struct_type;
我还初始化函数为其,其中一个参数是一个4个元素的数组。如何正确使用memcpy initalize array in struct?这样的东西不适合我:
struct_type* init_fcn(double array[4]){
struct _type* retVal;
retVal->par=malloc(sizeof(double)*4);
memcpy(retVal->par,&array);
return retVal;
}
我可以初始化值,但我thnik memcpy会更好,更快。你有什么想法如何正确做到这一点?
哦,我看到,MEMCPY也需要数组的大小,这是一样的给予的malloc大小 –
'的sizeof(*双)'是无稽之谈,并会导致在编译器错误。这不是[mcve] – StoryTeller
更像'memcpy(retVal-> par,array,sizeof(double)* 4)'。另外在malloc中,你可以使用'sizeof(double)'(一个元素的大小) –