Skip to content

Commit e8b5ce1

Browse files
author
Joshua Goller
committed
5.8-9 works
1 parent 2272801 commit e8b5ce1

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

ch-5/src/5.8-9.c

+10-11
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ int month_day(const int year, int yearday, int *const pmonth, int *const pday) {
7676

7777
int main() {
7878
int year, month, day, yearday;
79-
int *pday, *pmonth;
80-
pday = malloc(sizeof(int));
81-
pmonth = malloc(sizeof(int));
79+
int pday = 0;
80+
int pmonth = 0;
8281

8382
// daytab is an array of pointers to arrays of chars; these could now
8483
// be of different lengths as opposed to a "true" array of arrays
@@ -92,29 +91,29 @@ int main() {
9291
month = 6;
9392
day = 12;
9493
yearday = day_of_year(year, month, day);
95-
month_day(year, yearday, pmonth, pday);
94+
month_day(year, yearday, &pmonth, &pday);
9695
printf("%d/%d/%d was day #%d.\n", month, day, year, yearday);
97-
printf("day %d of year %d was %d/%d.\n\n", yearday, year, *pmonth, *pday);
96+
printf("day %d of year %d was %d/%d.\n\n", yearday, year, pmonth, pday);
9897

9998
// Invalid day
10099
day = 2000;
101100
yearday = day_of_year(year, month, day);
102-
month_day(year, yearday, pmonth, pday);
101+
month_day(year, yearday, &pmonth, &pday);
103102
printf("%d/%d/%d was day #%d.\n", month, day, year, yearday);
104-
printf("day %d of year %d was %d/%d.\n\n", yearday, year, *pmonth, *pday);
103+
printf("day %d of year %d was %d/%d.\n\n", yearday, year, pmonth, pday);
105104

106105
// Invalid month
107106
month = 87;
108107
yearday = day_of_year(year, month, day);
109-
month_day(year, yearday, pmonth, pday);
108+
month_day(year, yearday, &pmonth, &pday);
110109
printf("%d/%d/%d was day #%d.\n", month, day, year, yearday);
111-
printf("day %d of year %d was %d/%d.\n\n", yearday, year, *pmonth, *pday);
110+
printf("day %d of year %d was %d/%d.\n\n", yearday, year, pmonth, pday);
112111

113112
// Invalid year
114113
year = -2587;
115114
yearday = day_of_year(year, month, day);
116-
month_day(year, yearday, pmonth, pday);
115+
month_day(year, yearday, &pmonth, &pday);
117116
printf("%d/%d/%d was day #%d.\n", month, day, year, yearday);
118-
printf("day %d of year %d was %d/%d.\n\n", yearday, year, *pmonth, *pday);
117+
printf("day %d of year %d was %d/%d.\n\n", yearday, year, pmonth, pday);
119118
return 0;
120119
}

0 commit comments

Comments
 (0)