#include #include #include typedef unsigned char byte; const int name_max = 16; typedef struct _pet { char name[name_max], type[name_max]; int SSN; short int length, weight, birth_year; char sex; byte birth_month, birth_date; byte num_legs, num_eyes, num_fins; } pet; pet * new_pet(const char * NM, const char * TY, int S, int L, int W, int BY, int BM, int BD, char SX, int NL, int NE, int NF) { pet * p = (pet *)malloc(sizeof(pet)); strncpy(p->name, NM, name_max); strncpy(p->type, TY, name_max); p->SSN = S; p->length = L; p->weight = W; p->birth_year = BY; p->birth_month = BM; p->birth_date = BD; p->sex = SX; p->num_legs = NL; p->num_eyes = NE; p->num_fins = NF; return p; } void set_pet_name(pet * p, const char * nm) { strncpy(p->name, nm, name_max); } void print_pet(const pet * p) { printf("%.16s the %.16s, SSN %09d, sex %c, born %d-%d-%d\n", p->name, p->type, p->SSN, p->sex, p->birth_year, p->birth_month, p->birth_date); printf(" %d inches long, %d ounces, %d legs, %d eyes, %d fins.\n\n", p->length, p->weight, p->num_legs, p->num_eyes, p->num_fins); } int main() { pet * array[20]; int i; printf("sizeof(pet) is %d\n", sizeof(pet)); array[0] = new_pet("Tom", "cat", 635287163, 28, 9*16, 2004, 11, 3, 'M', 4, 2, 0); array[1] = new_pet("Stumpy", "dog", 32283673, 24, 12*16, 2003, 7, 4, 'M', 3, 2, 0); array[2] = new_pet("Goldie", "goldfish", 733283721, 2, 1, 2011, 12, 14, 'F', 0, 2, 5); array[3] = new_pet("Orangey", "goldfish", 273381721, 2, 2, 2012, 3, 28, 'M', 0, 2, 5); array[4] = new_pet("Millie", "dog", 131231912, 45, 67*16, 2006, 10, 10, 'F', 4, 2, 0); array[5] = new_pet("Fluffy", "elephant", 936253176, 110, 2023*16, 1997, 6, 13, 'F', 4, 2, 0); array[6] = new_pet("Cordelia","poodle", 43244365, 28, 160, 2009, 9, 23, 'F', 4, 1, 0); array[7] = new_pet("Jimbo", "snake", 236283931, 175, 41*16, 2007, 9, 25, 'M', 0, 2, 0); array[8] = new_pet("Polly", "parrot", 538901046, 10, 40, 1975, 5, 31, 'F', 2, 2, 0); array[9] = new_pet("Pugh", "conch", 065172614, 12, 52, 1999, 4, 17, 'M', 0, 0, 0); array[10]= new_pet("Hammy", "hamster", 185782394, 4, 4, 2010, 12, 15, 'F', 4, 2, 0); array[11]= NULL; printf("Cordelia is at address %d\n", array[6]); printf("Jimbo is at address %d\n", array[7]); printf("the difference is %d\n", (int)array[7] - (int)array[6]); set_pet_name(array[6], "Princess Cordelia Persephone Fluffiness Ponyweather Penelope Cutiepie Chorlmondeley-Featherstonehaugh III"); for (i = 0; array[i] != NULL; i += 1) print_pet(array[i]); for (i = 0; array[i] != NULL; i += 1) free(array[i]); }