Compare commits
No commits in common. "01348b14367e933707924121472001734de1b8a7" and "224300e1cf146572e16a776745bc3dd79e716ec1" have entirely different histories.
01348b1436
...
224300e1cf
14
lcthw/ex15.c
14
lcthw/ex15.c
@ -13,16 +13,8 @@ int main(int argc, char *argv[])
|
||||
int count = sizeof(ages) / sizeof(int);
|
||||
int i = 0;
|
||||
|
||||
char **first_name = &names[0];
|
||||
char **last_name = &names[count - 1];
|
||||
|
||||
printf("first_name=%p last_name=%p\n", first_name, last_name);
|
||||
printf("*first_name=%s *last_name=%s\n", *first_name, *last_name);
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// first way using indexing
|
||||
for (i = count - 1; i > -1; i--) {
|
||||
for (i = 0; i < count; i++) {
|
||||
printf("%s has %d years alive.\n", names[i], ages[i]);
|
||||
}
|
||||
|
||||
@ -33,14 +25,14 @@ int main(int argc, char *argv[])
|
||||
char **cur_name = names;
|
||||
|
||||
// second way using pointers
|
||||
for (i = count - 1; i > -1; i--) {
|
||||
for (i = 0; i < count; i++) {
|
||||
printf("%s is %d years old.\n", *(cur_name + i), *(cur_age + i));
|
||||
}
|
||||
|
||||
printf("---\n");
|
||||
|
||||
// third way, pointers are just arrays
|
||||
for (i = count - 1; i > -1; i--) {
|
||||
for (i = 0; i < count; i++) {
|
||||
printf("%s is %d years old again.\n", cur_name[i], cur_age[i]);
|
||||
}
|
||||
|
||||
|
71
lcthw/ex16.c
71
lcthw/ex16.c
@ -1,71 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct Person {
|
||||
char *name;
|
||||
int age;
|
||||
int height;
|
||||
int weight;
|
||||
};
|
||||
|
||||
struct Person *Person_create(char *name, int age, int height, int weight)
|
||||
{
|
||||
struct Person *who = malloc(sizeof(struct Person));
|
||||
assert(who != NULL);
|
||||
|
||||
who->name = strdup(name);
|
||||
who->age = age;
|
||||
who->height = height;
|
||||
who->weight = weight;
|
||||
|
||||
return who;
|
||||
}
|
||||
|
||||
void Person_destroy(struct Person *who)
|
||||
{
|
||||
assert(who != NULL);
|
||||
|
||||
free(who->name);
|
||||
free(who);
|
||||
}
|
||||
|
||||
void Person_print(struct Person *who)
|
||||
{
|
||||
printf("Name: %s\n", who->name);
|
||||
printf("\tAge: %d\n", who->age);
|
||||
printf("\tHeight: %d\n", who->height);
|
||||
printf("\tWeight: %d\n", who->weight);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// make two people structures
|
||||
struct Person *joe = Person_create("Joe Alex", 32, 64, 100);
|
||||
|
||||
struct Person *frank = Person_create("Frank Blank", 20, 72, 180);
|
||||
|
||||
// print them out and where they are in memory
|
||||
printf("Joe is at memory location %p:\n", joe);
|
||||
Person_print(joe);
|
||||
|
||||
printf("Frank is at memory location %p:\n", frank);
|
||||
Person_print(frank);
|
||||
|
||||
// make everyone age 20 years and print them again
|
||||
joe->age += 20;
|
||||
joe->height -= 2;
|
||||
joe->weight += 40;
|
||||
Person_print(joe);
|
||||
|
||||
frank->age += 20;
|
||||
frank->weight += 20;
|
||||
Person_print(frank);
|
||||
|
||||
// destroy them both so we clean up
|
||||
Person_destroy(joe);
|
||||
Person_destroy(frank);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user