Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

syntax - What is the reason for error while returning a structure in this C program?

My program intends to achieve this

(A) Write a C function named larger() that returns the later date of any two dates passed to it. For example, if the dates 10/9/2001 and 11/3/2001 are passed to larger(), the second date would be returned.

(B) Create the larger() function that was written for (A) in a complete unit. Store the date structure returned by larger() in a separate date structure and display the member values of the returned data structure.

I am working on this problem for my C Language course. I had everything going well (I thought), except that I kept getting "The larger date is: 0/0/0" no matter what I entered. So I started tinkering, and now I cannot get rid of the syntax error, or figure out my 0/0/0 problem. Obviously the dates aren't making it back through. I'm still pretty new at this (and very new at structs), so any help would be awesome!

The line that's giving me the syntax error is near the 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 will enter two dates, and the program will return the larger.
");
    while (user[0].month < 1 || user[0].month > 12)
         {printf("
Please enter the first month, 1-12: ");
         scanf("%d", &user[0].month);}
    while (user[0].day < 1 || user[0].day > 31)
          {printf("
Please enter the first day, 1-31: ");
          scanf("%d", &user[0].day);}
    while (user[0].year < 1)
          {printf("
Please enter the first year: ");
          scanf("%d)", &user[0].year);}
    printf("
Date entered: %d/%d/%d.
", user[0].month, user[0].day, user[0].year);

    while (user[1].month < 1 || user[1].month > 12)
         {printf("
Please enter the first month, 1-12: ");
         scanf("%d", &user[1].month);}
    while (user[1].day < 1 || user[1].day > 31)
          {printf("
Please enter the first day, 1-31: ");
          scanf("%d", &user[1].day);}
    while (user[1].year < 1)
          {printf("
Please enter the first year: ");
          scanf("%d)", &user[1].year);}
    printf("
Date entered: %d/%d/%d.

", user[1].month, user[1].day, user[1].year);

    DATES result[NUM] = larger(DATES user[NUM]);  /* Problem here */

    printf("The larger date is %d/%d/%d.

", 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]);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
DATES result[NUM] = larger(DATES user[NUM]);

What's this intended to do? DATES result[NUM] declares an array of DATES. (But each DATES contains only one date, which is confusing.) But despite being an array, it's initialized with a single object, the return value from larger. The argument to larger, DATES user[NUM], appears to be declaring user, a variable which already exists. It looks like you're trying to clarify to the compiler that user is an array of dates, but DATES doesn't go there and [NUM] appears to be indexing into the array, which you don't want.

Probably what you want is

DATES result = larger( user );

Also there are some serious style issues which will cause trouble later:

  • Array types for function parameters as in DATES larger(DATES[NUM]); are converted to pointers. On that line, NUM does nothing and the declaration is the same as DATES larger( DATES * );. Despite what some teachers may say, pointers and arrays are not the same thing. When the distinction is important, this style causes confusion. Use DATES larger( DATES * ) instead.

  • It doesn't make sense to capitalize DATES. Caps usually indicate a preprocessor macro.

  • Preprocessor macros are like a sledgehammer. Using them for simple numeric constants is less elegant than constructs like const int num = 2; or enum { num = 2 };

  • Descriptive variable names like input_limit and user_input would be better than num and user.

  • Don't rely on uninitialized data. user[0].month might be 6 after user is defined but not initialized. Always write a value before reading.

  • Braces should be kept visible. Grouping braces closely with the first and last words of the loop hides them, which is good if you think they're ugly, but then it's very very easy to incorrectly add a line to the loop, producing a hard-to-debug flow control error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...