0

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!

4
  • i think we already pointed out this problem in another of your questions. you are declaring a pointer to a pointer in struct aop, but you are allocating a bunch of struct person instead of struct person*. please read the answers that you get on here carefully and apply them accordingly. Commented Nov 17, 2014 at 12:03
  • @PhilippMurry I haven't asked this question before. Maybe someone else? Commented Nov 17, 2014 at 12:12
  • 1
    The same code was shown a few days ago here: stackoverflow.com/questions/26944311/… Is this from a homework? Commented Nov 17, 2014 at 12:14
  • struct person **data is "an array of pointers to struct person" while malloc((sizeof(struct person)) * max is malloc'ing "an array of struct person". You probably wan't to define data as struct person *data. Commented Nov 17, 2014 at 12:25

4 Answers 4

3

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

Comments

0

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

Comments

0

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.

Comments

0

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.

2 Comments

What goes in n_items_level2?
It depends on what you need. If you have an array of pointers, each pointer pointing to a single structure and not to an array, then n_items_level2 is 1. If you have to implement a 2D array, then n_items_level2 will be the number of columns.

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.