struct的问题,为什么我会收到此错误?

时间:2016-11-22 14:20:41

标签: c struct parameters

我在这里搜索了答案我看到了一些相关的帖子,但我无法理解这一切,我是C的新手,只是处理结构。我相信错误是结构处理的结果,但我不确定。

这是我在编写代码时遇到的错误,我将在其下面添加代码

30  38  C:\Users\PCPCPCPC\Documents\clock.c [Warning] 'struct dateAndtime'       

在参数列表

中声明

30 38 C:\ Users \ PCPCPCPC \ Documents \ clock.c [警告]其范围仅为此定义或声明,可能不是您想要的

42 20 C:\ Users \ PCPCPCPC \ Documents \ clock.c [错误]形式参数1的类型不完整

48 38 C:\ Users \ PCPCPCPC \ Documents \ clock.c [警告]'struct dateAndtime'在参数列表中声明

48 50 C:\ Users \ PCPCPCPC \ Documents \ clock.c [错误]参数1('当前')类型不完整

守则

#include <stdio.h>
#include <stdbool.h>

 //Global struct variables
 struct date
{
   int month;
   int day;
   int year;
};

struct time
{
   int seconds;
   int minutes;
   int hours;
};

struct dateAndTime
{
   struct date sDate;
   struct time sTime;
};

//prototypes
int numberOfDays (struct date d);
bool isLeapYear (struct date d);
struct date dateUpdate(struct date today);
struct time timeUpdate(struct time now);
struct dateAndTime clocKeeper(struct dateAndtime current);

int main()
{
    //struct dateAndTime dt, future;
    /*printf("please enter the correct date and time");
      printf("mm/dd/year hr:mm:sc");
    scanf("%i %i %i %i %i      %i",dt.sDate.month,dt.sDate.day,dt.sDate.year,dt.sTime.hours,dt.sTime.minutes,dt.sTime.seconds);*/

    struct dateAndTime dt1 = {{12, 31, 2004}, {23, 59, 59}};  
    struct dateAndTime dt2 = {{2, 28, 2008}, {23, 59, 58}};
    //future = clocKeeper(dt1);

    dt1 = clocKeeper (dt1);

    printf("the updated date is %i/%i/%i ", dt1.sDate.month, dt1.sDate.day,   dt1.sDate.year % 100);
   printf("the updated time is %.2i:%.2i:%.2i",  dt1.sTime.hours,dt1.sTime.minutes,dt1.sTime.seconds);
    }

 struct dateAndTime clocKeeper(struct dateAndtime current)
 {
     current.sTime = timeUpdate(current.sTime);

     if (current.sTime.hours == 0 && current.sTime.minutes == 59 &&  current.sTime.seconds == 59)
    current.sDate = dateUpdate(current.sDate);

    return current; 
 }

 struct time timeUpdate(struct time now)
 {
     ++now.seconds;   
     if ( now.seconds == 60 ) { // next minute 
         now.seconds = 0;
      ++now.minutes;
    if ( now.minutes == 60 ) { // next hour 
        now.minutes = 0;
       ++now.hours;
   if ( now.hours == 24 ) // midnight 
      now.hours = 0; 
    }
 }
   return now;
 }

 // Function to find the number of days in a month 
 int numberOfDays (struct date d)
 {
     int days;
      bool isLeapYear (struct date d);
      const int daysPerMonth[12] ={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,   30, 31 };
     if ( isLeapYear (d) == true && d.month == 2 )
           days = 29;
     else
          days = daysPerMonth[d.month - 1];

     return days;
  }

  // Function to determine if it's a leap year 
  bool isLeapYear (struct date d)
  {
     bool leapYearFlag;
     if ( (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0 )
            leapYearFlag = true; // It's a leap year 
     else
            leapYearFlag = false; // Not a leap year 
     return leapYearFlag;
 }

 struct date dateUpdate(struct date today)
 {
      struct date tomorrow;
      int numberOfDays (struct date d);
     if ( today.day != numberOfDays (today) ) {
         tomorrow.day = today.day + 1;
         tomorrow.month = today.month;
          tomorrow.year = today.year;
  }
  else if ( today.month == 12 ) { // end of year 
       tomorrow.day = 1;
      tomorrow.month = 1;
      tomorrow.year = today.year + 1;
}
 else { // end of month 
     tomorrow.day = 1;
     tomorrow.month = today.month + 1;
     tomorrow.year = today.year;
 }
  return tomorrow;
}

1 个答案:

答案 0 :(得分:0)

您在clocKeeper()函数中传递了错误的结构名称作为参数。

 struct dateAndTime clocKeeper(struct dateAndtime current)

正确的方法是: -

 struct dateAndTime clocKeeper(struct dateAndTime current)