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!
struct person **datais "an array of pointers to struct person" whilemalloc((sizeof(struct person)) * maxis malloc'ing "an array of struct person". You probably wan't to define data asstruct person *data.