1

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);
1
  • "struct contain check[];" are you able to use syntax like this ? What is the sizeof(check) here? Commented Feb 12, 2014 at 5:58

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

struct contain check = malloc(nsizeof(struct contain)); does not seem to work with for (i = 0; i != n ; i++) { check[i] = malloc(sizeof(struct contain)); memcpy( check[i], structArrayToBeCheck[i], sizeof(structArrayToBeCheck[i])); }
@Babbit: the alternative at the end of the answer is for a copy of the contiguous array. In that case there's no need to allocate each element separately - the entire array is copied in one shot. Socheck[0], check[1], etc. refer directly to struct contain objects, not pointers to such objects.
1
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);

6 Comments

JKB, How do you access the contents from there? printf( "check is %s\n", (*check)[1].suit.inner.option); does not print.
printf( "check is %s\n", check[1]->suit.inner.option); is valid for you
JKB, I tried that as well, but it did not print anything.
can you post your modified code with above my changes?
check and structArrayToBeCheck are already pointers when you don't use the array dereference. Remove the & in your memcpy.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.