问题描述
所以我有一个代码可以正常工作,但是我收到了“警告:从不兼容的指针类型传递‘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,". 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,int result)
,我收到“注意:预期为 'char (*)[10]' 但参数的类型为 'char *'” 我如何摆脱这些警告?
解决方法
在这次通话中
outsideBettingHistory(typeOfBet,betTypes->bets[0][typeOfBet],won,resultAmount);
第二个参数的类型为 char *
因为数据成员 bets 是一个二维类型的指针数组 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,int result) {