[英]Warning in C that says "expected ‘char *’ but argument is of type ‘char **’"
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:你好,所以我有一个主要功能,我声明两个字符串,要求 2 个输入并调用如下所示的函数,但我在 readString 和 showF 上有警告说“预期的 'char *' 但参数的类型是 'char **'”,我不明白为什么:
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: showF 功能代码:
void showF(PtList flights, char *originCode, char *destinationCode) {
if(flights != NULL && originCode != NULL && destinationCode != NULL) {
showFlightsWithRoute(flights, originCode, destinationCode);
}
}
showFlightsWithRoute code: showFlightsWithRoute 代码:
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");
}
}
}
The problem is that you send char** in functions taking as a parameter a char*.问题是您在以 char* 作为参数的函数中发送 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 需要一个 char*,而不是 char**,所以当你调用它时,你必须这样调用它:
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 相同的问题:您正在发送一个 char** 因为您正在使用 & :
showF(flights, &originCode, &destinationCode); //wrong
showF(flights, originCode, destinationCode); //right
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.