I think it is ok to do memcpy without malloc'ing check. However, I am unsure how shall we correct the code below, ie. How do we malloc struct array 'check'?

Here is the definition of the struct:

struct contain {
char* a;        //
int allowed;    //

struct suit {
   struct t {
          char* option;
          int count;
   } t;

   struct inner {
          char* option;
          int count;
   } inner;
} suit;
};

We initialized it with some values:

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[];

in main()

   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);

[Solved by Michael Burr and 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);
有帮助吗?

解决方案

This:

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

is invalid because you're copying an array of structures into an array of pointers.

Keep in mind that the set of structures that you dynamically allocate is not contiguous. The array of pointers is contiguous, but they point to things that are separately alllocated.

Try:

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

Also, if the structure copies are always allocated/deallocated as a block (like in your example), there's no need to allocate them separately. Just allocate enough space for the whole array:

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);

其他提示

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

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

might be this is safe way to allocate.what you want here n is how much array size you want.

and then

// Do not forget to free the memory when you are done:
for (int i = 0; i != n ; i++) {
    free(check[i]);
}
free(check);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top