so at first, I didnt know I had to use struct so I failed miserably and had to change my whole code but now it can't print anything :(
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct p {
char* name;
int year_of_birth;
char* sex;
struct p* mother;
struct p* father;
struct p* significant_other;
struct p** children;
}typedef Person;
this is where I define my structure
Person* person_constructor(char *name, int year_of_birth, char *sex);
Person* birth(char *name, int year_of_birth, char *sex, Person *mother);
void display_person(Person* p);
void marry_them(Person *p1, Person *p2);
void display_family(Person* family[], int n);
Person* sibling(Person p, int print);
here are my function definitions
int main() {
Person* p1 = person_constructor("Abbas", 1970, "male");
Person* p2 = person_constructor("Sidika", 1970, "female");
marry_them(p1, p2);
Person* p3 = person_constructor("Pinar", 1990, "female");
Person* p4 = birth("Siamak", 1990, "male", p2);
marry_them(p3, p4);
Person* p5 = birth("Guzide", 1990, "female", p2);
Person* p6 = person_constructor("Fatih", 1990, "male");
marry_them(p5, p6);
Person* p7 = birth("Berkecan", 2010, "male", p3);
Person* p8 = birth("Ekinsu", 2010, "female", p3);
Person* p9 = birth("Canim", 2010, "female", p5);
display_person(p1);
my main where I summon people, marry them etc and most important part,
return 0;
}
Person* person_constructor(char *name, int year_of_birth, char *sex)
{
Person *p = calloc(1,sizeof(*p));
strcpy(p->name, name);
p->year_of_birth = year_of_birth;
strcpy(p->sex, sex);
return p;
}
void display_person(Person* p)
{
printf("================\n");
printf("Name : %s \n",p->name);
printf("Sex : %s \n",p->sex);
printf("Year : %d \n",p->year_of_birth);
if (strcmp(p->father,"") == 0){
printf("Father : NA \n");
}
else {
printf("Father : %s \n", p->father);
}
if (strcmp(p->mother,"") == 0){
printf("Mother : NA \n");
}
else {
printf("Mother : %s \n", p->mother);
}
if (strcmp((const char *) p->significant_other, "") == 0){
printf("Sig.O : NA \n");
}
else {
printf("Sig.O : %s \n", p->significant_other);
}
if (strcmp(p->children,"") == 0){
printf("Child 1 : NA \n");
}
else {
printf("Child 1 : %s \n", p->children[0]);
}
if (strcmp(p->children,"") == 0){
printf("Child 2 : NA \n");
}
else {
printf("Child 2 : %s \n", p->children[1]);
}
}
void marry_them(Person *p1, Person *p2)
{
strcpy(p1->significant_other, p2->name);
strcpy(p2->significant_other, p1->name);
}
Person* birth(char *name, int year_of_birth, char *sex, Person *mother) {
Person *p = person_constructor(name, year_of_birth, sex);
strcpy(p->mother, mother->name);
strcpy(p->father, mother->significant_other);
return p;
}
void display_family(Person* family[], int n)
{
for (int i = 0;i<n;i++)
{
display_person(family[i]);
}
}
Person* sibling(Person p, int print)
{
}
these are my functions, I wrote them for char mother[20] kind of code instead of struct mother and I am tangled. I can't print out human info, it should look like this:
================
Name : Steven
Sex : male
Year : 1970
Father : NA
Mother : NA
Sig.O : Eva
Child 1: Laura
Child 2: Alice
================
above code is just representation without names I used in my main codes, just so you get what it is meant to do. Thanks in advance!