/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
struct Person
{
char surname[50];
unsigned int age;
struct Person *descendants;
unsigned int descendantsCount;
};
void zrobPotomka(struct Person *parent, unsigned int howMany)
{
parent->descendants = (struct Person*) malloc(howMany * sizeof(struct Person));
parent->descendantsCount = howMany;
for (int offset = 0; offset < howMany; offset++)
{
struct Person *currentDescendant = parent->descendants + offset;
strcpy(currentDescendant->surname, parent->surname);
currentDescendant->age = 0;
}
}
void printPersonData(struct Person *person)
{
printf("Nazwisko: %s, Wiek: %d, ilość potomków: %d \n", person->surname, person->age, person->descendantsCount);
//printf("Wiek: %d, ilość potomków: %d \n", person->age, person->descendantsCount);
for (int cnt = 0; cnt < person->descendantsCount; cnt++)
{
printf("Potomek %d:", cnt + 1);
struct Person *descendant = person->descendants + cnt;
printPersonData(descendant);
}
}
int main()
{
struct Person JanKowalski;
JanKowalski.age = 45;
strcpy(&JanKowalski.surname, "Kowalski");
JanKowalski.descendantsCount = 0;
printPersonData(&JanKowalski);
zrobPotomka(&JanKowalski, 2);
printPersonData(&JanKowalski);
return 0;
}