我在C中编写一些代码:
int main(){
char guess[15];
guess = helloValidation(*guess);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的功能是:
char[] helloValidation(char* des) {
do {
printf("Type 'hello' : ");
scanf("%s", &des);
}while (strcmp(des, "hello") != 0);
return des
}
Run Code Online (Sandbox Code Playgroud)
但它给了我这个错误:
incompatible types in assignment
Run Code Online (Sandbox Code Playgroud)
该guess数组由函数本身修改.然后,您尝试重新分配数组指针guess,从而导致错误.更不用说错误地尝试引用*guess或使用&des不正确.我建议你阅读C指针/数组概念.
#include <stdio.h>
#include <string.h>
char* helloValidation(char* des) {
do {
printf("Type 'hello' : ");
scanf("%s", des);
} while (strcmp(des, "hello") != 0);
return des;
}
int main() {
char guess[15];
helloValidation(guess);
return 0;
}
Run Code Online (Sandbox Code Playgroud)