#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Each person has two parents and two alleles
typedef struct person
    struct person *parents[2];
    char alleles[2];

const int GENERATIONS = 3;
const int INDENT_LENGTH = 4;

person *create_family(int generations);
void print_family(person *p, int generation);
void free_family(person *p);
char random_allele();

int main(void)
    // Seed random number generator

    // Create a new family with three generations
    person *p = create_family(GENERATIONS);

    // Print family tree of blood types
    print_family(p, 0);

    // Free memory

// Create a new individual with `generations`
person *create_family(int generations)
    // TODO: Allocate memory for new person
person *p= malloc(sizeof(person));

    // Generation with parent data
    if (generations > 1)
        // TODO: Recursively create blood type histories for parents
       p-> parents[0]=create_family(generations-1);
        p-> parents[1]=create_family(generations-1);
        // TODO: Randomly assign child alleles based on parents
         *p->parents[0]= *p->parents[0]->alleles[rand() %2];// line 55
         *p->parents[1]= *p->parents[1]->alleles[rand()%2];

    // Generation without parent data
        // TODO: Set parent pointers to NULL
        // TODO: Randomly assign alleles
        p->alleles[0] = random_allele();
        p->alleles[1] = random_allele();

    // TODO: Return newly created person
    return p;

// Free `p` and all ancestors of `p`.
void free_family(person *p)
    // TODO: Handle base case

    // TODO: Free parents

    // TODO: Free child

// Print each family member and their alleles.
void print_family(person *p, int generation)
    // Handle base case
    if (p == NULL)

    // Print indentation
    for (int i = 0; i < generation * INDENT_LENGTH; i++)
        printf(" ");

    // Print person
    printf("Generation %i, blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
    print_family(p->parents[0], generation + 1);
    print_family(p->parents[1], generation + 1);

char random_allele()
    int r = rand() % 3;
    if (r == 0)
        return 'A';
    else if (r == 1)
        return 'B';
        return 'O';

This is the code^ Below is the error i'm getting in the terminal- indirection requires pointer operand ('int' invalid)

tried using help50 but is doesn't really help point out the error and only says to focus on line 55. Any help would be appreciated, thanks a ton!

1 Answer 1


When randomly assigning an allele to the person from their parents', you should go into the person's allele (-> already does that and replace the *., no need to reuse the star), and assign that to one of each parent's allele. *p->parents[0]= *p->parents[0]->alleles[rand() %2];// line 55 should therefore be changed to : p->alleles[0]= p->parents[0]->alleles[rand() %2];// line 55 basically removing the star to fix going into an address twice, and fixing the mistake (assuming some typo) of assigning the person's parents to the allele instead of the person's allele. Side advice : Idk if checking if person == NULL in a function is good practice, you might end up with segmentation faults pretty easily,it's better to take it a step further and check for the parents' pointer or generations number to avoid passing NULL as an argument.

  • Thanks a lot for taking time the time to help me out! Will do:)
    – user01
    Dec 8, 2021 at 17:20

You must log in to answer this question.

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