malloc() and free()

#include 
#include
#include

main( )
{ struct animal
{ char name[25];
char breed[25];
int age;
} *pet1, *pet2;

pet2 = malloc(sizeof(struct animal));
strcpy(pet2->name,"Krystal");
strcpy(pet2->breed,"German Shepard");
pet2->age = 4;

/* now print out the data described above */

printf("%s is a %s, and is %d years old.\n",pet2->name,pet2->breed, pet2->age);
pet1 = pet2; /* pet1 now points to the same structure that pet3 points to */
free(pet2); /* this frees up one structure */

printf("%s is a %s, and is %d years old.\n",pet1->name,pet1->breed, pet1->age);
}

No comments:

Post a Comment