Haxify
Haxify

Reputation: 429

Allocating dynamic array of structures within a structure

I have the following structures:

struct date {
    int year;
    int month;
    int day;
};

struct person{
    char name[64];
    struct date birthday;
};

struct aop {
    int max;
    struct person **data;
};  

I tried malloc for data within aop structure like this: (no errors occurred here)

struct aop *create_aop(int max) {
    struct aop *s = malloc(sizeof(struct aop));
    s->max = max;
    s->data = malloc((sizeof(struct person)) * max);
    return s;
}  

But when I tried accessing "data" in other part of the code, such as this:

a->data[len]->birthday.year = birthday.year;  

I got errors.
Am I doing malloc the wrong way, or am I accessing the data incorrectly?

Thank you in advance!

Upvotes: 0

Views: 84

Answers (4)

n. m. could be an AI
n. m. could be an AI

Reputation: 119877

Am I doing malloc the wrong way, or am I accessing the data incorrectly?

Yes. Study this incredibly informative diagram:

Type *****var = malloc (sizeof(Type****) * n_items);
/*   -----                         ----                   */
/*     |                             |                    */
/*     +---> n stars                 +---> n-1 stars      */

If you have more than one star, you are not done yet. You need to allocate the data at the next level of indirection:

for (i = 0; i < n_items; ++i)
{
   var[i] = malloc (sizeof(Type***) * n_items_level2);
   /*                          ---                        */
   /*                           |                         */
   /*                           +---> n-2 stars           */

If you still have stars, you are not done yet. You need to allocate the data at the next level of indirection in a nested loop:

   for (j = 0; j < n_items_level2; ++j)
   {
       var[i][j] = malloc (sizeof(Type**) * n_items_level3);

and so on until you run out of stars.

Upvotes: 0

rfermi
rfermi

Reputation: 179

I've tried to create the same structure here, and I couldnt acess that structure Person.

Since you're willing to create multi person entries, how about creating a linked list? Like:

struct aop {
  int max;
  struct person **data;
};
struct person{
  char name[64];
  struct date birthday;
  struct person *nextPerson;
};

Probably it will work.

Upvotes: 0

Jeegar Patel
Jeegar Patel

Reputation: 27210

In aop structure you do not need double pointer for struct person. so

struct aop {
    int max;
    struct person **data;
};  

change struct person **data;

to

struct person *data;

And while using that use it as below way.

a->data[len].birthday.year = birthday.year;  

Upvotes: 3

russtone
russtone

Reputation: 410

Field data in your aop structure is array of poiters, so at first you need to allocate memory for pointers:

s->data = malloc((sizeof(struct person*)) * max);

And then in loop you need to allocate memory for each structure:

for(i = 0; i < max; i++) {
    s->data[i] = malloc(sizeof(struct person));
}

Upvotes: 0

Related Questions