0
person *p = malloc(sizeof(person));
// If there are still generations left to create
if (generations > 1)
{
    // Create two new parents for current person by recursively calling create_family
    person *parent0 = create_family(generations - 1);
    person *parent1 = create_family(generations - 1);

    // TODO: Set parent pointers for current person
    p->parents[0] = parent0;
    p->parents[1] = parent1;
    // TODO: Randomly assign current person's alleles based on the alleles of their parents
    p->alleles[0] = parent0->alleles[rand() % 2];
    p->alleles[1] = parent1->alleles[rand() % 2];
}

1 Answer 1

0

No because you arent calling create family against p you are calling it to assign to parent0 and parent1 then you are assigning those pointers into the parents array of p.

presumably create_family is creating a chunk of memory that parent0 and parent1 are pointing to.

you can check by printing it out. cast p to a long and print its value after the malloc and then through the code as you are curious and you will see that its address does not change.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .