我是编程新手。
此函数(验证)的目的是验证用户是否在命令提示符中输入了正确的命令:-h,-k,-f,-e或-d。如果用户没有这样做,则程序打印错误消息,向主函数返回布尔值零,然后结束程序。如果用户使用其中一个命令,则返回1,程序在main函数中继续。
但是我从编译器收到此错误消息:
指针和整数之间的比较
('bool(*)(char *,char *,int)'和'int')
if(validate == 1){“
这是我的功能:
bool validate(char* file_check, char* keyfile_check, int argc) {
int validationcleared;
if (strcmp(file_check, "-h") != 0 && strcmp(file_check, "-k") != 0 &&
strcmp(file_check, "-f") != 0 && strcmp(file_check, "-e") != 0 &&
strcmp(file_check, "-d") != 0) {
printf("please use a valid file\n");
return 0;
}
else if (strcmp(file_check, "-k") == 0 && (argc < 3)) {
printf("please use a digit character for keyfile\n");
return 0;
}
else {
return 1;
}
}
这是我的主要功能:
int main(int argc, char* argv[]) {
int validationcleared;
if (argc < 2) {
printf("Usage: partb 1 2 3 \n");
exit(-1);
}
char* filename = argv[1];
char* keyfile = argv[2];
validate(filename,keyfile, argc);
if (validate==1) {
printf("validation test passed\n");
}
else {
printf("validation test not passed\n");
return 0;
}
}
答案 0 :(得分:4)
这是初学者中常见的错误:
validate(filename, keyfile, argc);
if (validate == 1) {
// ....
如果要检查函数的返回值,则应使用实际的函数调用,而不是函数的名称。像这样:
if (validate(filename, keyfile, argc) == 1) {
// ....
或者,如果您仍想分几步完成:
bool result = validate(filename, keyfile, argc);
if (result == 1) {
// ....