#include #include int main(int argc, char ** argv) { struct date {short day, month, year;}; typedef struct date Date; typedef struct date * Datep; // create date using struct date struct date today = {6,2,2017}; printf("today: %02d/%02d/%04d\n", today.month, today.day, today.year); // create date using typedef Date tomorrow = {7,2,2017}; printf("tomorrow: %02d/%02d/%04d\n", tomorrow.month, tomorrow.day, tomorrow.year); // create date pointer to existing date Datep todayp = &today; printf("todayp: %02d/%02d/%04d\n", todayp->month, todayp->day, todayp->year); // create new date using pointer to allocated memory Datep yesterday = malloc(sizeof(Datep)); yesterday->month = 2; yesterday->day = 5; yesterday->year = 2017; printf("yesterday: %02d/%02d/%04d\n", yesterday->month, yesterday->day, yesterday->year); Date nextyear = today; nextyear.year = 2018; printf("nextyear: %02d/%02d/%04d\n", nextyear.month, nextyear.day, nextyear.year); printf("today: %02d/%02d/%04d\n", today.month, today.day, today.year); // create a date with just the first two values specified Date birthday = {5, 2}; printf("birthday: %02d/%02d/%04d\n", birthday.month, birthday.day, birthday.year); // create a date with just the last value specified Date graduation = {.year = 2017}; printf("graduation: %02d/%02d/%04d\n", graduation.month, graduation.day, graduation.year); // create a recursive data structure typedef struct person { char * first; char * last; Date birthday; struct person * friend; } Person; // create and initialize a person Person john = { "John", "Smith", { 4, 7, 1776 }}; printf("john: %s %s, %02d/%02d/%04d\n", john.first, john.last, john.birthday.month, john.birthday.day, john.birthday.year); // create and initialize a person with a friend Person mary = { "Mary", "Johnson", { 4, 7, 1776 }, &john}; printf("mary: %s %s, %02d/%02d/%04d\n", mary.first, mary.last, mary.birthday.month, mary.birthday.day, mary.birthday.year); printf("mary's friend: %p %s\n", mary.friend, mary.friend->first); // create a pointer to a person Person * jane = malloc(sizeof(Person)); char * f = "Jane"; char * l = "Doe"; Date d = {5, 6, 1999}; jane->first = f; jane->last = l; jane->birthday = d; printf("jane: %s %s, %02d/%02d/%04d\n", jane->first, jane->last, jane->birthday.month, jane->birthday.day, jane->birthday.year); // comment these out and run valgrind free(yesterday); free(jane); }