initial import
[staff/goc4/2018bti7061.git] / c-5-structuresFiles / examples / c-structures / structures4.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 typedef struct{
5 int year;
6 int month;
7 int day;
8 } date;
9
10 typedef struct{
11 char* name;
12 char* firstname;
13 date birth;
14 date death;
15
16 } person;
17
18
19 int main(){
20 person* emmanuel = (person*) malloc(sizeof(person));
21
22
23 emmanuel->name="Benoist";
24 emmanuel->firstname="Emmanuel";
25 emmanuel->birth=(date){1960,10,29};
26 emmanuel->death=(date){0,0,0};
27
28 person* p2 = (person*) malloc(sizeof(person));
29 p2->name = "Muster";
30 p2->firstname = "Hans";
31 p2->birth=(date){1890,1,1};
32 p2->death=(date){1918,11,11};
33
34 person* list_of_persons[20];
35 list_of_persons[0]=emmanuel;
36 list_of_persons[1]=p2;
37
38 int nb_of_persons = 2;
39 int i;
40 for(i=0;i < nb_of_persons; i++){
41 person* person = list_of_persons[i];
42 printf("*** Person %d ***\n",i);
43 printf("%s %s \n",person->firstname,person->name);
44 printf("Birth date :%d.%d.%d\n",person->birth.day,person->birth.month,person->birth.year);
45 if(person->death.year !=0){
46 printf("Death date :%d.%d.%d\n",person->death.day,person->death.month,person->death.year);
47 }
48 }
49 }
50 /* Output:
51 *** Person 0 ***
52 Emmanuel Benoist
53 Birth date :29.10.1960
54 *** Person 1 ***
55 Hans Muster
56 Birth date :1.1.1890
57 Death date :11.11.1918
58 */