Created
May 31, 2010 04:34
-
-
Save jonsterling/419536 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Jon’s full name: Jonathan Sterling | |
| Jon’s original age: 17 | |
| adding 30 years… | |
| Jon’s new age: 47 | |
| reverting to infancy… | |
| Jon’s new age: 0 | |
| Hi! I’m Jonathan Sterling! Hello World! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Person jon; | |
| jon.firstName = "Jonathan"; | |
| jon.lastName = "Sterling"; | |
| jon.age = 17; | |
| char * fullName; | |
| person_getFullName(&fullName, jon); | |
| printf("Jon’s full name: %s\n", fullName); | |
| free(fullName); | |
| printf("Jon’s original age: %i\n", jon.age); | |
| printf("adding 30 years…\n"); | |
| person_addToAge(&jon, 30); | |
| printf("Jon’s new age: %i\n", jon.age); | |
| printf("reverting to infancy…\n"); | |
| person_revertToInfancy(&jon); | |
| printf("Jon’s new age: %i\n", jon.age); | |
| person_sayHello(jon); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| char * person_getFullName(char ** dest, Person p) { | |
| *dest = (char *)malloc((strlen(p.firstName) + strlen(p.lastName) + 1) * sizeof(char)); | |
| strcpy(*dest, p.firstName); | |
| strcat(*dest, " "); | |
| strcat(*dest, p.lastName); | |
| return *dest; | |
| } | |
| Person * person_addToAge(Person * p, int years) { | |
| p->age += years; | |
| return p; | |
| } | |
| Person * person_revertToInfancy(Person *p) { | |
| p->age = 0; | |
| return p; | |
| } | |
| void person_sayHello(Person p) { | |
| char * fullName; | |
| printf("Hi! I'm %s! Hello World!\n", person_getFullName(&fullName, p)); | |
| free(fullName); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typedef struct person_t { | |
| char * firstName; | |
| char * lastName; | |
| int age; | |
| } Person; | |
| char * person_getFullName(char ** dest, Person p); | |
| Person * person_addToAge(Person * p, int years); | |
| Person * person_revertToInfancy(Person * p); | |
| void person_sayHello(Person p); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment