【问题标题】:error: conflicting types, error: type of formal parameter 2 is incomplete错误:类型冲突,错误:形式参数 2 的类型不完整
【发布时间】:2019-05-16 21:05:21
【问题描述】:

我正在编写一个代码来计算 2 个给定日期之间的天数差异。请注意,每个月被视为等于 30 天,每年被视为等于 360 天。

我收到以下警告/错误,但我似乎不明白为什么:

Warning&Errors

这是我的代码:

#include <stdio.h>

int dif_days(struct Date Date1, struct Date Date2);

struct Date
{
    int Day;
    int Month;
    int Year;
};

int main()
{
    struct Date Date1;
    struct Date Date2;

    printf("\n Please enter the first date: ");
    scanf("%d %d %d ", &Date1.Day, &Date1.Month, &Date1.Year);

    printf("\n Please enter the second date: ");
    scanf("%d %d %d ", &Date2.Day, &Date2.Month, &Date2.Year);

    int diff = dif_days(Date1, Date2);
    printf("\n The difference in days is: %d \n", diff);

    return 0;
}

int dif_days(struct Date Date1, struct Date Date2)
{
    // variable declaration 
    int difference;
    int Day, Month, Year; // The final days/months/years

    // for the days
    if (Date1.Day > Date2.Day)
        Day = Date1.Day - Date2.Day;
    else    
        Day = Date2.Day - Date1.Day;

    // for the months
    if (Date1.Month > Date2.Month) 
        Month = Date1.Month - Date2.Month;
    else
        Month = Date2.Month - Date1.Month;

    // for the years
    if (Date1.Year > Date2.Year)
        Year = Date1.Year - Date2.Year;
    else        
        Year = Date2.Year - Date1.Year;       

    difference = Day + Month*30 + Year*360;                

    return difference;
}

【问题讨论】:

  • 您的原型定义int dif_days(struct Date Date1, struct Date Date2); 使用了此时未定义的结构数据。所以把它移到struct Date的定义之后。除此之外,您的计算错误的方式很多;你用“16 12 2018”和“16 12 2018”测试了吗? “2018 年 31 月 12 日”和“2019 年 1 月 1 日”?
  • @KlausGütter 我已经将结构 Date 移到原型定义上。我尝试测试“16 12 2018”和“16 12 2018”,结果为零。但是,您向我推荐的其他组合似乎没有给出有效答案,因此我将再次查看我的算法。感谢您指出!
  • @KlausGütter 我设法重新编写了代码,现在它就像一个魅力,但仍然存在一个小问题。如果用户输入“31 12 2018”和“1 1 2019”,答案是 0。我是否应该在我的函数中添加一个 if 语句来检查用户是否输入了超过 30 天,以便他们可以转换为30 因为这是变量day 可以拥有的最大值。
  • 为什么不计算360*year + 30*month + day 这两天的差额?
  • @KlausGütter 这就是我所做的。我分别计算了每个日期的天数,然后我做了一个if 这样的声明:if (days_of_date1 &gt; days_of_date2) difference = days_of_date1 - days_of_date2; else difference = days_of_date2 - days_of_date1;

标签: c function structure


【解决方案1】:

在dif_days()的函数原型中使用之前,需要先声明struct Date。

您可以将struct 的整个定义移动到函数原型之前,也可以通过在函数原型之前添加struct 来使用前向声明:

struct Date;

此外,您需要从scanf() 格式字符串中删除尾随空格字符。 This plays havoc with interactive input, and does not do what people usually seem to expect. 请注意,%d 指令会自动忽略前导空格字符,事实上,不 忽略前导空格的唯一 scanf() 指令是 %c、%n 和 @987654332 @。

当我们讨论scanf() 的主题时,您应该检查对scanf() 的调用返回的值,以确保输入符合预期。一旦格式字符串中的匹配失败,scanf() 将继续前进,而不会在其余变量中存储任何内容。当代码尝试使用不确定的值时,无效的输入将导致未定义的行为。如果输入了三个数字,scanf() 将在此处返回 3,并且对于基本验证,您可以在继续之前检查用户是否确实输入了三个数字。

【讨论】:

  • 您的回答似乎解决了警告和错误。但是,如果您不介意回答,我还有一个问题要问。我也可以使用 &i 代替 &d 吗?如果是这样,该更改是否会对程序的整体性能造成任何不良行为?
  • %i 指令允许以 C 整数常量格式输入,允许输入十六进制或八进制文字。除非您想要这种特殊行为,否则这可能会导致混乱。我会坚持使用%d 以避免意外,因为您可能想要的行为是只期望十进制输入。特别是,像010 这样的八进制文字可以用%i 输入,存储的值将是8,这可能会让人感到意外。
猜你喜欢
  • 2018-04-20
  • 2016-02-07
  • 2013-03-25
  • 1970-01-01
  • 1970-01-01
  • 2014-03-10
  • 2012-10-04
  • 2013-04-24
  • 1970-01-01
相关资源
最近更新 更多