简体   繁体   English

将 arguments 传递给 function 时出现警告

[英]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?所以我有一个代码,它必须如何工作,但我得到“警告:从不兼容的指针类型传递'outsideBettingHistory'的参数2”,这是为什么呢? 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?此外,对于void outsideBettingHistory(int x, char betChosen[0][10], bool won, int result)我得到“注意:预期的 'char (*)[10]' 但参数的类型是 'char *'”如何摆脱这些警告?

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 .第二个参数的类型为char * ,因为数据成员 bets 是char *类型的指针的二维数组,并且您选择了与 bets[ bets[0][0] ] 相同的数组bets[0][typeOfBet]的元素bets[0][0]因为typeOfBet0初始化。 That is you passed to the function a pointer to the first character of the string literal "Red" .也就是说,您向 function 传递了一个指向字符串文字"Red"的第一个字符的指针。

But the second parameter of the function outsideBettingHistory但是function的第二个参数在outsideBettingHistory

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

has the type char ( * )[10] .具有类型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.您应该自己决定要传递给 function 的内容以及 function 应该做什么。

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如果假设 function outsideBettingHistory必须处理字符串文字(二维数组的元素),则声明 function 像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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