0

I'm working on lab5, inheritance, and I'm trying to decipher why I'm getting this error when trying to compile:

inheritance.c:52:32: error: incompatible pointer types assigning to 'struct person *' 
from 'person *(int)' (aka 'struct person *(int)') [-Werror,-Wincompatible-pointer-types]

new_person->parents[0] = create_family;

Here is the function as it exists in the overall program. I also included the lines where it defines the structure "person" and sets a global constant 3.

    typedef struct person
    {
        struct person *parents[2];
        char alleles[2];
    }
    person;



const int GENERATIONS = 3;


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

    // Generation with parent data
    if (generations > 1)
    {
        // TODO: Recursively create blood type histories for parents
        generations--;
        new_person->parents[0] = create_family;
        new_person->parents[1] = create_family;

        // TODO: Randomly assign child alleles based on parents
        new_person->alleles[0] = random_allele;
        new_person->alleles[1] = random_allele;
    }

    // Generation without parent data
    else if (y < (2^(generations - 1)))
    {
        y++;
        // TODO: Set parent pointers to NULL
        new_person->parents[0] = NULL;
        new_person->parents[1] = NULL;

        // TODO: Randomly assign alleles
        new_person->alleles[0] = random_allele;
        new_person->alleles[1] = random_allele;
    }

    // TODO: Return newly created person
    return new_person;
}

If the function create_family returns a pointer to a person, and the parents subtype in the person structure is also a pointer to a person, why doesn't this line of code work? Why the error?

new_person->parents[0] = create_family;
2

create_family is the name of your function. In order to call it, be sure you don't forget the ( ) with the argument.

If I wanted to create a family with generations number of generations, then I'd call create_family(generations)

2
  • Thanks so much! Now I'm getting a similar error for the lines where I call the random_allele function. That one is still confusing me, because the random_allele function takes no input, and returns a char – WhatEvenIsCoding Jan 30 at 23:01
  • 1
    You still need the parentheses so that C distinguishes the function from a variable. – Cliff B Jan 30 at 23:31

You must log in to answer this question.

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