我认为没有malloc'ing检查就可以执行memcpy。但是,我不确定如何更正下面的代码,即。我们如何malloc结构数组'检查'?如何在memcpy之前创建malloc结构数组
这里是结构的定义:
struct contain structArrayToBeCheck[] = {
{
.a = "John",
.allowed = 1,
.suit = {
.t = {
.option = "ON",
.count = 7
},
.inner = {
.option = "OFF",
.count = 7
}
}
},
{
.a = "John",
.allowed = 1,
.suit = {
.t = {
.option = "ON",
.count = 7
},
.inner = {
.option = "OK",
.count = 7
}
}
},
{
.a = "John",
.allowed = 1,
.suit = {
.t = {
.option = "ON",
.count = 7
},
.inner = {
.option = "OFF",
.count = 7
}
}
},
};
struct contain check[];
在主
()
int i;
int n = sizeof(structArrayToBeCheck)/sizeof(struct contain);
printf("There are %d elements in the array.\n", n);
struct contain **check = malloc(n*sizeof(struct contain *));
for (i = 0; i != n ; i++) {
check[i] = malloc(sizeof(struct contain));
}
memcpy(&check, &structArrayToBeCheck, sizeof(structArrayToBeCheck));
//printf("check is %s\n", check[1]->suit.inner.option);
[迈克尔·伯尔和解决:
struct contain {
char* a; //
int allowed; //
struct suit {
struct t {
char* option;
int count;
} t;
struct inner {
char* option;
int count;
} inner;
} suit;
};
我们与一些值初始化它JKB]
int i;
int n = sizeof(structArrayToBeCheck)/sizeof(struct contain);
printf("There are %d elements in the array.\n", n);
struct contain *check = malloc(n*sizeof(struct contain));
memcpy(check, structArrayToBeCheck, sizeof(structArrayToBeCheck));
// do things with check[0], check[1], ... check[n-1]
printf("check is %s\n", check[1].suit.inner.option);
free(check);
“struct contains check [];”你能够使用这样的语法吗?什么是sizeof(支票)在这里? –