2014-02-12 25 views
1

我认为没有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); 
+0

“struct contains check [];”你能够使用这样的语法吗?什么是sizeof(支票)在这里? –

回答

1

此:

memcpy(&check, &structArrayToBeCheck, sizeof(structArrayToBeCheck)); 

是无效的,因为你复制结构的数组指针数组。

请记住,您动态分配的一组结构不是连续的。指针数组是连续的,但它们指向分开的所有东西。

尝试:

for (i = 0; i != n ; i++) { 
    check[i] = malloc(sizeof(struct contain)); 
    memcpy(check[i], ArrayToBeCheck[i], sizeof(ArrayToBeCheck[i])); 
} 

而且,如果结构副本始终分配/释放的块(如在你的例子),也没有必要单独对其进行分配。只需为整个阵列分配足够的空间:

struct contain *check = malloc(n*sizeof(struct contain)); 

memcpy(check, structArrayToBeCheck, sizeof(structArrayToBeCheck)); 

// do things with check[0], check[1], ... check[n-1] 

free(check); 
+0

**结构包含*检查=的malloc(N *的sizeof(结构含)); ** 似乎并不与 \t工作** for(i = 0; i!= n; i ++){[i] = malloc(sizeof(struct contains)); \t的memcpy(检查[I],structArrayToBeCheck [I],的sizeof(structArrayToBeCheck [I])); ** \t} – Babbit

+1

@Babbit:在所述应答的端部的另一种方法是对连续的数组的副本。在这种情况下,不需要分别分配每个元素 - 整个阵列被一次性复制。因此,'checkcheck [0]','check [1]'等直接指向'struct contains'对象,而不是指向这些对象的指针。 –

1
struct contain **check = malloc(n*sizeof(struct contain *)); 

for (int i = 0; i != n ; i++) { 
    check[i] = malloc(sizeof(struct contain)); 
} 

可能这是allocate.what你想在这里n是你要多少数组的大小安全的方式。

然后

// Do not forget to free the memory when you are done: 
for (int i = 0; i != n ; i++) { 
    free(check[i]); 
} 
free(check); 
+0

JKB, 你如何从那里访问内容? printf(“check is%s \ n”,(* check)[1] .suit.inner.option);不打印。 – Babbit

+1

'printf(“check is%s \ n”,check [1] - > suit.inner.option);'对你有效 –

+0

JKB,我也试过,但没有打印任何东西。 – Babbit