back to version with long main func
This commit is contained in:
parent
728a7c5bd8
commit
0a0fc453c5
55
ex15.c
55
ex15.c
@ -1,16 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
void print_names_ages(int count, char *names[], int ages[], const char *fmt)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
for(i = 0; i < count; i++) {
|
|
||||||
printf(fmt, names[i], ages[i]);
|
|
||||||
}
|
|
||||||
printf("---\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
// create two arrays we care about
|
// create two arrays we care about
|
||||||
@ -21,13 +10,47 @@ int main(int argc, char *argv[])
|
|||||||
};
|
};
|
||||||
// safely get the size of ages
|
// safely get the size of ages
|
||||||
int count = sizeof(ages) / sizeof(int);
|
int count = sizeof(ages) / sizeof(int);
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
print_names_ages(count, names, ages, "%s has %d years alive.\n");
|
// first way using indexing
|
||||||
print_names_ages(count, names, ages, "%s is %d years old.\n");
|
for(i = 0; i < count; i++) {
|
||||||
print_names_ages(count, names, ages, "%s is %d years old again.\n");
|
printf("%s has %d years alive.\n",
|
||||||
print_names_ages(count, names, ages, "%s lived %d years so far.\n");
|
names[i], ages[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("---\n");
|
||||||
|
|
||||||
|
// setup the pointers to the start of the arrays
|
||||||
|
int *cur_age = ages;
|
||||||
|
char **cur_name = names;
|
||||||
|
|
||||||
|
// second way using pointers
|
||||||
|
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 = 0; i < count; i++) {
|
||||||
|
printf("%s is %d years old again.\n",
|
||||||
|
cur_name[i], cur_age[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("---\n");
|
||||||
|
|
||||||
|
// fourth way with pointers in a stupid complex way
|
||||||
|
for(cur_name = names, cur_age = ages;
|
||||||
|
(cur_age - ages) < count;
|
||||||
|
cur_name++, cur_age++)
|
||||||
|
{
|
||||||
|
printf("%s lived %d years so far.\n",
|
||||||
|
*cur_name, *cur_age);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("---\n");
|
||||||
|
|
||||||
int i;
|
|
||||||
char **arg = argv;
|
char **arg = argv;
|
||||||
for(i = 0; i < argc; i++) {
|
for(i = 0; i < argc; i++) {
|
||||||
printf("argument %d is '%s' (address = %p)\n", i, *arg, arg);
|
printf("argument %d is '%s' (address = %p)\n", i, *arg, arg);
|
||||||
|
Loading…
Reference in New Issue
Block a user