[英]How can I fix the given compiler errors
我对 C 语言相当陌生,我无法弄清楚如何修复您可以在问题末尾找到的这些编译器错误。 在第一个错误时,它说它需要一个 int * 但给出了 int ** 类型,这两者之间有什么区别? 如果你能详细解释一下就好了
#include <stdio.h>
#include <limits.h>
// Array format [DD,MM,YYYY]
int date[3];
int secondDate[3];
int correctFormat = 0;
// Constant texts
const char DAY_TEXT[] = "Enter the day of the first date: ";
const char MONTH_TEXT[] = "Enter the month of the first date: ";
const char YEAR_TEXT[] = "Enter the year of the first date: ";
const char DAY_ERROR[] = "Please enter the day in correct format DD";
const char MONTH_ERROR[] = "Please enter the month in correct format MM";
const char YEAR_ERROR[] = "Please enter year in correct format YYYY";
// ANSI color codes
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define RESET "\033[0m"
int checkDigits(int i)
{
/*
Fastest way to check number of digits in given integer
*/
if (i < 0)
i = (i == INT_MIN) ? INT_MAX : -i;
if (i < 10)
return 1;
if (i < 100)
return 2;
if (i < 1000)
return 3;
if (i < 10000)
return 4;
if (i < 100000)
return 5;
if (i < 1000000)
return 6;
if (i < 10000000)
return 7;
if (i < 100000000)
return 8;
if (i < 1000000000)
return 9;
}
void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
{
while (correctFormat == 0)
{
printf("%s", text);
scanf("%d", &dateVal);
if (checkDigits(dateVal) == 4)
{
correctFormat = 1;
}
else
{
printf(RED " %s \n" RESET, errorText);
}
}
correctFormat = 0;
}
int main()
{
dateInput(2, date[0], DAY_TEXT, DAY_ERROR);
dateInput(2, date[1], MONTH_TEXT, MONTH_ERROR);
dateInput(4, date[2], YEAR_TEXT, YEAR_ERROR);
dateInput(2, secondDate[0], DAY_TEXT, DAY_ERROR);
dateInput(2, secondDate[1], MONTH_TEXT, MONTH_ERROR);
dateInput(4, secondDate[2], YEAR_TEXT, YEAR_ERROR);
// Checking if the second date's year is less than the first one
if (secondDate[2] < date[2])
{
printf(GREEN "Second date is earlier than the first %d \n", checkDigits(secondDate[2]));
}
else if (secondDate[2] > date[2])
{
printf(RED "Second date is not earlier than the first \n");
}
// Checking if the second date's month is less than the first one
else if (secondDate[1] < date[1])
{
printf(GREEN "Second date is earlier than the first \n");
}
else if (secondDate[1] > date[1])
{
printf(RED "Second date is not earlier than the first \n");
}
// If months are equal then check the day
else if (secondDate[1] == date[1])
{
if (secondDate[0] < date[0])
{
printf(GREEN "Second date is earlier than the first \n");
}
else if (secondDate[0] == date[0])
{
printf(RED "The dates are basically the same \n ");
}
}
return 0;
}
task2cp.c: In function ‘dateInput’:
task2cp.c:58:17: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int **’ [-Wformat=]
58 | scanf("%d", &dateVal);
| ~^ ~~~~~~~~
| | |
| | int **
| int *
task2cp.c:59:25: warning: passing argument 1 of ‘checkDigits’ makes integer from pointer without a cast [-Wint-conversion]
59 | if (checkDigits(dateVal) == 4)
| ^~~~~~~
| |
| int *
task2cp.c:25:21: note: expected ‘int’ but argument is of type ‘int *’
25 | int checkDigits(int i)
| ~~~~^
task2cp.c: In function ‘main’:
task2cp.c:73:22: warning: passing argument 2 of ‘dateInput’ makes pointer from integer without a cast [-Wint-conversion]
73 | dateInput(2, date[0], DAY_TEXT, DAY_ERROR);
| ~~~~^~~
| |
| int
task2cp.c:53:39: note: expected ‘int *’ but argument is of type ‘int’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~^~~~~~~~~
task2cp.c:73:27: warning: passing argument 3 of ‘dateInput’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
73 | dateInput(2, date[0], DAY_TEXT, DAY_ERROR);
| ^~~~~~~~
task2cp.c:53:55: note: expected ‘char *’ but argument is of type ‘const char *’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~~^~~~~~
task2cp.c:73:37: warning: passing argument 4 of ‘dateInput’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
73 | dateInput(2, date[0], DAY_TEXT, DAY_ERROR);
| ^~~~~~~~~
task2cp.c:53:68: note: expected ‘char *’ but argument is of type ‘const char *’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~~^~~~~~~~~~~
task2cp.c:75:22: warning: passing argument 2 of ‘dateInput’ makes pointer from integer without a cast [-Wint-conversion]
75 | dateInput(2, date[1], MONTH_TEXT, MONTH_ERROR);
| ~~~~^~~
| |
| int
task2cp.c:53:39: note: expected ‘int *’ but argument is of type ‘int’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~^~~~~~~~~
task2cp.c:75:27: warning: passing argument 3 of ‘dateInput’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
75 | dateInput(2, date[1], MONTH_TEXT, MONTH_ERROR);
| ^~~~~~~~~~
task2cp.c:53:55: note: expected ‘char *’ but argument is of type ‘const char *’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~~^~~~~~
task2cp.c:75:39: warning: passing argument 4 of ‘dateInput’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
75 | dateInput(2, date[1], MONTH_TEXT, MONTH_ERROR);
| ^~~~~~~~~~~
task2cp.c:53:68: note: expected ‘char *’ but argument is of type ‘const char *’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~~^~~~~~~~~~~
task2cp.c:77:22: warning: passing argument 2 of ‘dateInput’ makes pointer from integer without a cast [-Wint-conversion]
77 | dateInput(4, date[2], YEAR_TEXT, YEAR_ERROR);
| ~~~~^~~
| |
| int
task2cp.c:53:39: note: expected ‘int *’ but argument is of type ‘int’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~^~~~~~~~~
task2cp.c:77:27: warning: passing argument 3 of ‘dateInput’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
77 | dateInput(4, date[2], YEAR_TEXT, YEAR_ERROR);
| ^~~~~~~~~
task2cp.c:53:55: note: expected ‘char *’ but argument is of type ‘const char *’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~~^~~~~~
task2cp.c:77:38: warning: passing argument 4 of ‘dateInput’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
77 | dateInput(4, date[2], YEAR_TEXT, YEAR_ERROR);
| ^~~~~~~~~~
task2cp.c:53:68: note: expected ‘char *’ but argument is of type ‘const char *’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~~^~~~~~~~~~~
task2cp.c:79:28: warning: passing argument 2 of ‘dateInput’ makes pointer from integer without a cast [-Wint-conversion]
79 | dateInput(2, secondDate[0], DAY_TEXT, DAY_ERROR);
| ~~~~~~~~~~^~~
| |
| int
task2cp.c:53:39: note: expected ‘int *’ but argument is of type ‘int’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~^~~~~~~~~
task2cp.c:79:33: warning: passing argument 3 of ‘dateInput’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
79 | dateInput(2, secondDate[0], DAY_TEXT, DAY_ERROR);
| ^~~~~~~~
task2cp.c:53:55: note: expected ‘char *’ but argument is of type ‘const char *’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~~^~~~~~
task2cp.c:79:43: warning: passing argument 4 of ‘dateInput’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
79 | dateInput(2, secondDate[0], DAY_TEXT, DAY_ERROR);
| ^~~~~~~~~
task2cp.c:53:68: note: expected ‘char *’ but argument is of type ‘const char *’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~~^~~~~~~~~~~
task2cp.c:81:28: warning: passing argument 2 of ‘dateInput’ makes pointer from integer without a cast [-Wint-conversion]
81 | dateInput(2, secondDate[1], MONTH_TEXT, MONTH_ERROR);
| ~~~~~~~~~~^~~
| |
| int
task2cp.c:53:39: note: expected ‘int *’ but argument is of type ‘int’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~^~~~~~~~~
task2cp.c:81:33: warning: passing argument 3 of ‘dateInput’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
81 | dateInput(2, secondDate[1], MONTH_TEXT, MONTH_ERROR);
| ^~~~~~~~~~
task2cp.c:53:55: note: expected ‘char *’ but argument is of type ‘const char *’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~~^~~~~~
task2cp.c:81:45: warning: passing argument 4 of ‘dateInput’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
81 | dateInput(2, secondDate[1], MONTH_TEXT, MONTH_ERROR);
| ^~~~~~~~~~~
task2cp.c:53:68: note: expected ‘char *’ but argument is of type ‘const char *’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~~^~~~~~~~~~~
task2cp.c:83:28: warning: passing argument 2 of ‘dateInput’ makes pointer from integer without a cast [-Wint-conversion]
83 | dateInput(4, secondDate[2], YEAR_TEXT, YEAR_ERROR);
| ~~~~~~~~~~^~~
| |
| int
task2cp.c:53:39: note: expected ‘int *’ but argument is of type ‘int’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~^~~~~~~~~
task2cp.c:83:33: warning: passing argument 3 of ‘dateInput’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
83 | dateInput(4, secondDate[2], YEAR_TEXT, YEAR_ERROR);
| ^~~~~~~~~
task2cp.c:53:55: note: expected ‘char *’ but argument is of type ‘const char *’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~~^~~~~~
task2cp.c:83:44: warning: passing argument 4 of ‘dateInput’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
83 | dateInput(4, secondDate[2], YEAR_TEXT, YEAR_ERROR);
| ^~~~~~~~~~
task2cp.c:53:68: note: expected ‘char *’ but argument is of type ‘const char *’
53 | void dateInput(int correctDigits, int dateVal[], char text[], char errorText[])
| ~~~~~^~~~~~~~~~~
int * 类型是指向整数的指针。 int ** 类型是指向整数指针的指针。
基本上想象一下,当你在你的记忆中创建一个变量时,你会给它一个名字,这将是你引用它的方式。 如果你愿意,你可以得到指向变量的指针,指向变量的指针会记住你的变量在内存中的索引(比如 0xf325... 和类似的)。 这意味着您可以将指针传递给程序的任何函数/部分,并且您将始终在存储变量的这部分内存中工作!
您更有可能必须观看解释由值和引用传递的变量的指南。
例如,在第一个警告中,它说您正在传递 int* 的 int** insthead。 正如您在代码中看到的 Argument 传递了 &dateVal ,但 dateVal 可能是 int* 类型,传入 int* 的指针将作为 int** 的指针返回。 一个可能的解决方法是传入 dateVal 之前没有“&”,因此您将根据要求传递 int*。 我不知道它是否会按照您的预期执行,但这就是您遇到错误的原因!
int dateVal[]
作为参数只是int *dateVal
另一种写法。 由于dateVal
已经是指向您要写入的位置的指针,因此您需要scanf("%d", dateVal)
除非您没有为dateVal
传递指针。 dateInput(2, date[0], ...)
应该是dateInput(2, &date[0], ...)
或dateInput(2, date+0, ...)
。
最后, char text[]
和char errorText[]
应该添加const
因为您不修改指向的值,并允许函数接受常量值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.