hi all,
I was reading through an old C tutorial and I tried this program on my pc:
why is this program's output:Code:#include <stdio.h> #include <string.h> #include <stdlib.h> 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); }
Krystal is a German Shepard, and is 4 years old.
is a German Shepard, and is 4 years old.
instead of what I supposed it should be?:
Krystal is a German Shepard, and is 4 years old.
is a German Shepard, and is years old.
why does the 4 remain after free() ?
thanks a lot



LinkBack URL
About LinkBacks



