简体   繁体   中英

How can I fix the given compiler errors

I am fairly new to the C language and I can't figure out how to fix these compiler errors that you can find at the end of the question. At the first error it says that it expects a int * but has given the type int ** What's the difference between these two? Would be great if you could explain in detail

#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[])
      |                                                               ~~~~~^~~~~~~~~~~

The int * type is a Pointer to an Integer. The int ** type is a Pointer to a Pointer to an Integer.

Basically Imagine that when you create a Variable in your memory, you'll give it a Name that will be the way you refer to that. If you want, you can get the Pointer to the Variable, a Pointer to a Variable will memorize the index of your variable in memory (like 0xf325... and similar). That means you can pass the Pointer to any function/part of program, and you will always work in this part of the memory where your variable is stored in!

It's more likely you have to watch a guide that explains variables passed by Value and by Reference.

For example in the first Warning it says you are passing a int** insthead of int*. As you can see in your code as Argument you passed &dateVal , but dateVal is probably of type int* and passing in the Pointer of a int* will return as a pointer of a int**. A possible fix will be to pass in dateVal without the "&" before it, so you will pass a int* as requested. I don't know if it will do what you expect to, but that's why you have the Error!

int dateVal[] as a parameter is just another way of writing int *dateVal . Since dateVal is already a pointer to where you want to write, you want scanf("%d", dateVal)

Except you don't pass a pointer for dateVal . dateInput(2, date[0], ...) should be dateInput(2, &date[0], ...) or dateInput(2, date+0, ...) .

Finally, char text[] and char errorText[] should have const added since you don't modify the pointed values, and to permit the function to accept constant values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM