/* structs_2.c create a struct definition declare, initialize and print and free a DYNAMIC struct variable using the -> notation Tim Hoffman F03-15113 */ #include #include #include typedef struct { char *name; int year; /* 1=frosh, 2=soph etc */ } Person; /* Person is now a data type */ int main( int argc, char *argv[]) { Person *p; /* the pointer */ /* allocate the object */ p = malloc( sizeof(Person) ); /* malloc a string for the name field */ p->name = malloc( strlen("Herman Munster") + 1 ); /* copy name and year into the object */ strcpy( p->name, "Herman Munster" ); p->year = 5; /* grad student */ /* print */ printf("name: %s, year: %d\n", p->name, p->year ); free( p->name ); /* the string */ free( p ); /* the struct */ return 0; } /* EOF */