【问题标题】:issues with struct, why do I get this error?结构问题,为什么会出现此错误?
【发布时间】:2026-02-02 02:15:02
【问题描述】:

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

这是我尝试编译代码时遇到的错误,我将在其下方添加代码

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 ('current') 类型不完整

守则

#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;
}

【问题讨论】:

  • Nested function in C的可能重复
  • C 区分大小写!在要求检查错别字之前!
  • 我已经检查过了,我看不到任何与大小写有关的拼写错误,而且这与 C 中的嵌套函数不同。

标签: c struct parameters


【解决方案1】:

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

 struct dateAndTime clocKeeper(struct dateAndtime current)

正确的方法是:-

 struct dateAndTime clocKeeper(struct dateAndTime current)

【讨论】:

  • 非常感谢 5 小时的疏忽,我没有看到,太糟糕了。
  • 这是我的荣幸。我建议您不要使用完整的结构名称,而是使用 typedef 作为别名。
  • 好吧,我是结构概念的新手,我会继续阅读
最近更新 更多