我在编译此源时遇到问题。 这是我的源:如何解决“从'char *'到'char'错误的无效转换?
#include <iostream>
#include <string>
using namespace std;
char pass[7],d;
int v;
int isvalid(char pass);
int main(int argc, char** argv) {
cout<<"Enter Password :";
gets(pass);
cout<<endl;
v=isvalid (pass);
if(v==1){
cout<<"The Password is Correct . You can Come in"<<endl;
}
else{
cout<<"The Password in InCorrect ! You Can't Come In"<<endl;
}
system("PAUSE");
return 0;
}
/*-----------Functions--------------*/
int isvalid(char pass){
d="Pokerface";
if(pass==d){
return 1;
}
else{
return 0;
}
}
应该从用户那里得到一个密码,并参考isValid功能检查,并说这是正确的或没有,但编译器(DEV C++ 5)显示我这些错误:
In function 'int main(int, char**)':
14 17 [Error] invalid conversion from 'char*' to 'char' [-fpermissive]
8 5 [Note] initializing argument 1 of 'int isvalid(char)'
In function 'int isvalid(char)':
26 3 [Error] invalid conversion from 'const char*' to 'char' [-fpermissive]
28 recipe for target 'main.o' failed
问题是什么?请帮帮我。
变量'pass'的类型是'字符*''不char'。它们之间有区别。 'char *'是指向char的指针。将函数'isvalid'的参数类型更改为'char *'而不是'char',然后修改方法。 –
char是1个字节,char *是一个4字节的指针,指向字节数组中的第一个字符。 –