int main(int argc, char *argv[]) { // make two people, createPerson returns a Person* struct Person *joe = createPerson("Joe", 26, 70, 189); struct Person *susan = createPerson("Susan", 28, 66, 115); // %p is used to print pointers (memory location) printf("Joe is at memory location: %p\n", joe); printPerson(joe); printf("Susan is at memory location: %p\n", susan); printPerson(susan); // age everyone 10 years joe->age += 10; joe->height -= 1; joe->weight += 10; susan->age += 10; susan->height -= 2; susan->weight += 5; printPerson(joe); printPerson(susan); // call this method to clean up memory. no delete? destroyPerson(joe); destroyPerson(susan); return 0; }
int main() { struct Person *joe = createPerson("Joe Alex", 32, 64, 140); struct Person *jack = createPerson("Jack Blank", 20, 72, 180); printf("Joe is at memory location %p\n", joe); printPerson(joe); printf("Jack is at memory location %p\n", jack); printPerson(jack); //printPerson(NULL); //Make everyone age 20 years. joe->Age += 20; joe->Height -=2; joe->Weight +=40; jack->Age += 20; jack->Weight += 20; printPerson(joe); printPerson(jack); struct Person j = *joe; struct Person j2 = j; printf("Joe is at memory location %p\n", &j); j.Age += 3; printf("Joe is %i years old\n", j.Age); printf("Joe is at memory location %p\n", &j2); printf("Joe is %i years old\n", j2.Age); destroyPerson(joe); destroyPerson(jack); //destroyPerson(NULL); return 0; }
int main(void) { int result; person * person1; person * person2; person * person3 = NULL; /*Start createPerson Test*/ printf("\nTesting createPerson function...\n"); person1 = createPerson(); printf("person1 pointer value: %p\n", &person1); person2 = createPerson(); printf("person2 pointer value: %p\n", &person2); if(&person1 != &person2) { printf("Success! pointers are referencing different mem locations. EXPECTED\n\n"); } else { printf("Fail... pointers are referencing same mem location.\n\n"); } /*End createPerson Test*/ /*Start destroyPerson Test*/ printf("Testing destroyPerson function...\n"); destroyPerson(person1); person1 = NULL; printf("person1 deleted... ptr value: %p\n", &person1); printf("trying to delete again...\n"); destroyPerson(person1); printf("NOTE: must set the pointer to NULL after using destroyPerson, or program will crash due to invalid pointer\n\n"); /*End destroyPerson Test*/ /*Start printPerson Test*/ printf("Testing printPerson function...\n"); printf("printing the destroyed person1...\n"); printPerson(person1); printf("printing the valid person2...\n"); printPerson(person2); printf("\n"); /*End printPerson Test*/ /*Start changeInfo Test*/ printf("Testing changeInfo function...\n") ; printf("Attempting to changeInfo on destroyed person1...\n"); if(changeInfo(person1, "Justin","Fuerth",25)) { printf("Error! person1 is NULL\n"); } else { printf("Successfully changed info... something went wrong...\n"); } printf("Attempting to changeInfo on valid person2...\n"); if(changeInfo(person2, "Justin","Fuerth",25)) { printf("Error! person2 is NULL... that's not right\n"); } else { printf("Successfully changed info for person2!\n"); } printf("Printing person2 again...\n"); printPerson(person2); printf("\n"); /*End changeInfo Test*/ /*recreating person1 for further testing purposes*/ printf("Creating person1 again and giving it values: Stephen, Fuerth, 62\n\n"); person1 = createPerson(); changeInfo(person1, "Stephen", "Fuerth", 62); /*Start equalTo Test*/ printf("Testing the equalTo function...\n"); printf("Testing equalTo with NULL pointer person3...\n"); if((result = equalTo(person3,person3))>-1) { if(result == 0) { printf("the two people are different!\n"); } else { printf("the two people are equal!\n"); } } else { printf("There was an error with the equalTo function. EXPECTED\n"); } printf("Testing equalTo with one NULL and one valid...\n"); if((result = equalTo(person1,person3))>-1) { if(result == 0) { printf("the two people are different!\n"); } else { printf("the two people are equal!\n"); } } else { printf("There was an error with the equalTo function. EXPECTED\n"); } printf("Testing equalTo function with two valid, different people...\n"); if((result = equalTo(person1,person2))>-1) { if(result == 0) { printf("the two people are different! EXPECTED\n"); } else { printf("the two people are equal!\n"); } } else { printf("There was an error with the equalTo function.\n"); } printf("Testing equalTo function with two valid, equal people...\n"); if((result = equalTo(person1,person1))>-1) { if(result == 0) { printf("the two people are different!\n"); } else { printf("the two people are equal! EXPECTED\n"); } } else { printf("There was an error with the equalTo function.\n"); } printf("\n"); /*End equalTo Test*/ /*Start copyInfo Test*/ printf("Testing copyInfo function...\n"); printf("Attempting to copy into a NULL pointer...\n"); if(copyInfo(person1,person3)) { printf("there was an error with the copyInfo function... EXPECTED\n"); } printf("Attempting to copy from a NULL pointer...\n"); if(copyInfo(person3,person1)) { printf("there was an error with the copyInfo function... EXPECTED\n"); } /*creating an empty person3 to copy info into*/ person3 = createPerson(); printf("Attempting to copy from valid person1 to valid person3...\n"); if(copyInfo(person1,person3)) { printf("there was an error with the copyInfo function...\n"); } else { printf("SUCCESS! EXPECTED\n"); } printf("Printing person3... expected: Stephen Fuerth, 62\n"); printPerson(person3); /*End copyInfo Test*/ /*freeing all memory to check memleaks with valgrind*/ destroyPerson(person1); destroyPerson(person2); destroyPerson(person3); /*Aug 26 8:53 - valgrind shows no memory leaks*/ printf("\n"); return 0; }