Search code examples
cc-stringsstring-literalsfunction-declaration

Warnings when passing arguments to a function


So I have a code that works how it has to work, but I am getting the "warning: passing argument 2 of 'outsideBettingHistory' from incompatible pointer type", why is that? My project is huge so I will only rewrite parts that play the role in the warning, so you can paste it yourself and get the same errors.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct Bet {
    char* bets[3][2];
} Bet;

void outsideBettingHistory(int x, char betChosen[0][10], bool won, int result) {
    //You can ignore what is inside this function
    FILE *f;
    f = fopen("bettingHistory.txt", "a");
    if(!f) {
        printf("\nThe bettingHistory.txt not found or unable to open");
        exit(0);
    }
    if(won) {
        fprintf(f, "%s %s", "Bet type: ", betChosen[0]);
        fprintf(f, ". Won %d credits\n", result);
    }
    if(!won) {
        fprintf(f, "%s %s", "Bet type: ", betChosen[0]);
        fprintf(f, ". Lost %d credits\n", result);
    }
    fclose(f);
}

int betColours(int balance, Bet* betTypes) {
      //A lot of stuff that have nothing to do with the warning

      int typeOfBet = 0; //This is the example of 3 variables that this function would give to the outsideBettingHistory(); function
      bool won = false;
      int resultAmount = 8;

      outsideBettingHistory(typeOfBet, betTypes->bets[0][typeOfBet], won, resultAmount);
      return balance;
}


int main() {
     int balance = 100;
     Bet betTypes = { .bets={{"Red", "Black"}, {"Even", "Odd"}, {"1 to 18", "19 to 36"}}};
     betColours(balance, &betTypes);
}

Also, for void outsideBettingHistory(int x, char betChosen[0][10], bool won, int result) I am getting "note: expected 'char (*)[10]' but argument is of type 'char *'" How do I get rid of these warnings?


Solution

  • In this call

    outsideBettingHistory(typeOfBet, betTypes->bets[0][typeOfBet], won, resultAmount);
    

    the second argument has the type char * because the data member bets is a two-dimensional array of pointers of the type char * and you selected the element of the array bets[0][typeOfBet] that is the same as bets[0][0] because typeOfBet was initialized by 0. That is you passed to the function a pointer to the first character of the string literal "Red".

    But the second parameter of the function outsideBettingHistory

    void outsideBettingHistory(int x, char betChosen[0][10], bool won, int result) {
    

    has the type char ( * )[10].

    And the types are not compatible. So the compiler issues an error.

    You should decide for yourself what you are trying to pass to the function and what the function shall do.

    If it is supposed that the function outsideBettingHistory must deal with a string literal (an element of the two-dimensional array) then declare the function like

    void outsideBettingHistory(int x, const char *betChosen, bool won, int result) {