/* structs_2.c create a struct definition declare, initialize and print and free a DYNAMIC struct variable using the -> notation */ #include #include #include typedef struct { char *name; int age; } Person; /* Person is now a data type */ int main(int argc, char *argv[]) { Person *ptr; /* the pointer */ /* allocate the object */ ptr = (Person *)malloc(sizeof(Person)); (*ptr).name = (char *)malloc((strlen("Mark Stehlik") + 1) * sizeof(char)); /* copy name and year into the object */ strcpy((*ptr).name, "Mark Stehlik" ); ptr->age = 47; /* print */ printf("name= %s, age= %d\n", ptr->name, ptr->age); free(ptr->name); /* the string */ free(ptr); /* the struct */ return 0; }