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

前端 未结 5 1495
粉色の甜心
粉色の甜心 2020-12-21 14:54

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

5条回答
  •  梦毁少年i
    2020-12-21 15:16

    @Rüppell'sVulture You highlighted his errors but the code is too erroneous. I have worked on a simpler solution of it. Have a look.

    @iMPose27 Please see the following code and let me know in case you run in some difficulty

    // includes
    #include 
    #include 
    
    // macros
    #define NUM 2
    
    // structure Definitions
    struct Dates
    {
           int month;
           int day;
           int year;
    };
    
    // typedefs
    typedef struct Dates DATES;
    
    // function declarations
    DATES* larger(DATES[NUM]);
    
    // function definitions
    int main(int argc, char* argv[])
    {
        DATES user[NUM];    // array of NUM DATES
        DATES *result=NULL;
        int i=0;
        printf("\nPlease Enter Two Dates, The program will evaluate and return the later date of the two dates passed to it\n\n");
        for(;i 12);
    
            do{
                  printf("Please enter the day, 1-31:\t");
                  scanf("%d", &user[i].day);
            }while (user[i].day < 1 || user[i].day > 31);
    
            do{           
                  printf("Please enter the year: \t");
                  scanf("%d)", &user[i].year);
            }while (user[i].year < 1);
    
            printf("\nDate %d entered: %d/%d/%d.\n\n", i+1, user[i].month, user[i].day, user[i].year);
    
        } 
    
        if((result=larger(user))==NULL)
            printf("The two dates passed, date1: %d/%d/%d and date2: %d/%d/%d are the same.\n",user[0].month, user[0].day, user[0].year, user[1].month, user[1].day, user[1].year); 
        else
            printf("%d/%d/%d is the later date of the two dates passed\n",result->month, result->day, result->year); 
    
        return 0;
    }
    
    DATES* larger(DATES more[NUM])
    {        
          int days0, 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 (days0 > days1)
            return more;        
          else if (days1 > days0)
            return more+1;
          else
            return 0;       
    }
    

提交回复
热议问题