我有一个关于memcpy()的基本问题:
我有一个结构,其中有几个数组作为其成员.
我想将结构中的数据复制到缓冲区内存中(通过malloc()分配)
我看到了这个分段错误.我在这个实现中做错了吗?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10
struct temp {
int en;
int one[MAX];
int two[MAX];
};
int main()
{
struct temp *cpy;
int *buffer, i;
for(i=0; i<MAX; i++) {
cpy->one[i] = i;
cpy->two[i] = i * i;
}
buffer = malloc(3 * MAX * sizeof(int));
memcpy(buffer, cpy, sizeof(struct temp));
}
Run Code Online (Sandbox Code Playgroud)
如何将完整数据复制到缓冲区?