Home > Net >  Warning in C that says "expected ‘char *’ but argument is of type ‘char **’"
Warning in C that says "expected ‘char *’ but argument is of type ‘char **’"

Time:07-10

Hello so I have a main fuction where I declare two strings, ask for 2 inputs and call a function that looks like the following, but i have warnings on readString and showF that say "expected ‘char *’ but argument is of type ‘char **’" and i cant understand why:

char *originCode;
char *destinationCode;

printf("Insert origin airport code: ");
readString(&originCode, 3);

printf("Insert destination airport code: ");
readString(&destinationCode, 3);
printf("\n");

showF(flights, &originCode, &destinationCode);

readString function code:

void readString(char *charArr, unsigned int maxChars) {
    fgets(charArr, maxChars, stdin);
    removeNewline(charArr); /* modifies in-place*/
}

showF function code:

void showF(PtList flights, char *originCode, char *destinationCode) {
    if(flights != NULL && originCode != NULL && destinationCode != NULL) {
        showFlightsWithRoute(flights, originCode, destinationCode);
    }
}

showFlightsWithRoute code:

void showFlightsWithRoute(PtList flights, char *originCode, char *destinationCode) {
    //create a new list
    PtList list = listCreate();

    //search flights for flights that have the same origin and destination as the ones we want and add them to the new list
    int size;
    listSize(flights, &size);
    Flight flight;

    for(int i = 0; i < size; i  ) {
        listGet(flights, i, &flight);
        if(equalsStringIgnoreCase(flight.originAirport, originCode) == 1 && equalsStringIgnoreCase(flight.destinationAirport, destinationCode) == 1)
            listAdd(list, i, flight);
    }

    //print information about the flights
    int newListSize;
    listSize(list, &newListSize);
    Flight elem;
    
    if(newListSize == 0) {
        printf("Flight data not available for route <%s> ==> <%s>", originCode, destinationCode);
    }

    else {
        printf("----------------------------------Flights With Desired Route----------------------------------\n");
        for(int i = 0; i < size; i  ) {
            listGet(list, i, &elem);
            flightPrint(&elem);
            printf("\n");
        }
    }
}

CodePudding user response:

The problem is that you send char** in functions taking as a parameter a char*.

Here is your main :

char *originCode;
char *destinationCode;

printf("Insert origin airport code: ");
readString(&originCode, 3); //needs char*

printf("Insert destination airport code: ");
readString(&destinationCode, 3); //needs char*
printf("\n");

showF(flights, &originCode, &destinationCode); //needs char*

The function readString takes a char*, not a char**, so when you call it, you have to call it this way :

readString(originCode, 3); //no & before originCode or it sends a char**
...
readString(originCode, 3); //same comment than above

It is the same issue with showF : you are sending a char** because you are using & :

showF(flights, &originCode, &destinationCode); //wrong
showF(flights, originCode, destinationCode); //right
  •  Tags:  
  • c
  • Related