因此,我有一个代码可以正常工作,但是我得到了“警告:从不兼容的指针类型传递'outsideBettingHistory'的参数2”,这是为什么呢?我的项目很大,因此我将只重写在警告中起作用的部分,因此您可以自己粘贴并得到相同的错误。
#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);
}
另外,因为void outsideBettingHistory(int x, char betChosen[0][10], bool won, int result)
我得到了“注意:预期的'char(*)[10]'但参数的类型为'char *'”,我如何摆脱这些警告?
在这次通话中
outsideBettingHistory(typeOfBet, betTypes->bets[0][typeOfBet], won, resultAmount);
第二个参数具有类型,char *
因为数据成员押注是该类型的指针的二维数组,char *
并且您选择的数组元素bets[0][typeOfBet]
与相同,bets[0][0]
因为它typeOfBet
是由初始化的0
。那就是您将指向字符串文字第一个字符的指针传递给该函数"Red"
。
但是函数的第二个参数 outsideBettingHistory
void outsideBettingHistory(int x, char betChosen[0][10], bool won, int result) {
具有类型char ( * )[10]
。
并且类型不兼容。因此,编译器发出错误。
您应该自己决定要传递给函数的内容以及函数应执行的操作。
如果假设函数outsideBettingHistory
必须处理字符串文字(二维数组的元素),则将函数声明为
void outsideBettingHistory(int x, const char *betChosen, bool won, int result) {
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句