syntax - What is the reason for error while returning a structure in this C program? -
my program intends achieve this
(a) write c function named larger()
returns later date of 2 dates passed it. example, if dates 10/9/2001 , 11/3/2001 passed larger()
, second date returned.
(b) create larger() function written (a) in complete unit. store date structure returned larger() in separate date structure , display member values of returned data structure.
i working on problem c language course. had going (i thought), except kept getting "the larger date is: 0/0/0" no matter entered. started tinkering, , cannot rid of syntax error, or figure out 0/0/0 problem. dates aren't making through. i'm still pretty new @ (and new @ structs), awesome!
the line that's giving me syntax error near bottom of main():
dates result[num] = larger(dates user[num]);
the code in full:
#include <stdio.h> #define num 2 struct dates { int month; int day; int year; }; typedef struct dates dates; dates larger(dates[num]); dates more; int main() { dates user[num]; printf("you enter 2 dates, , program return larger.\n"); while (user[0].month < 1 || user[0].month > 12) {printf("\nplease enter first month, 1-12: "); scanf("%d", &user[0].month);} while (user[0].day < 1 || user[0].day > 31) {printf("\nplease enter first day, 1-31: "); scanf("%d", &user[0].day);} while (user[0].year < 1) {printf("\nplease enter first year: "); scanf("%d)", &user[0].year);} printf("\ndate entered: %d/%d/%d.\n", user[0].month, user[0].day, user[0].year); while (user[1].month < 1 || user[1].month > 12) {printf("\nplease enter first month, 1-12: "); scanf("%d", &user[1].month);} while (user[1].day < 1 || user[1].day > 31) {printf("\nplease enter first day, 1-31: "); scanf("%d", &user[1].day);} while (user[1].year < 1) {printf("\nplease enter first year: "); scanf("%d)", &user[1].year);} printf("\ndate entered: %d/%d/%d.\n\n", user[1].month, user[1].day, user[1].year); dates result[num] = larger(dates user[num]); /* problem here */ printf("the larger date %d/%d/%d.\n\n", result[0].month, result[0].day, result[0].year); system("pause"); return 0; } dates larger(dates more[num]) { int days0; int days1; days0 = (more[0].month*31)+(more[0].day)+(more[0].year*365); days1 = (more[1].month*31)+(more[1].day)+(more[1].year*365); if (days1 > days0) {more[0] = more[1];} return (more[0]); }
dates result[num] = larger(dates user[num]);
what's intended do? dates result[num]
declares array of dates
. (but each dates
contains 1 date, confusing.) despite being array, it's initialized single object, return value larger
. argument larger
, dates user[num]
, appears declaring user
, variable exists. looks you're trying clarify compiler user
array of dates, dates
doesn't go there , [num]
appears indexing array, don't want.
probably want is
dates result = larger( user );
also there serious style issues cause trouble later:
array types function parameters in
dates larger(dates[num]);
converted pointers. on line,num
nothing , declaration samedates larger( dates * );
. despite teachers may say, pointers , arrays not same thing. when distinction important, style causes confusion. usedates larger( dates * )
instead.it doesn't make sense capitalize
dates
. caps indicate preprocessor macro.preprocessor macros sledgehammer. using them simple numeric constants less elegant constructs
const int num = 2;
orenum { num = 2 };
descriptive variable names
input_limit
,user_input
betternum
,user
.don't rely on uninitialized data.
user[0].month
might 6 afteruser
defined not initialized. write value before reading.braces should kept visible. grouping braces closely first , last words of loop hides them, if think they're ugly, it's very easy incorrectly add line loop, producing hard-to-debug flow control error.
Comments
Post a Comment