我是编程新手。
此功能(验证)的目的是验证用户是否在命令提示符中输入了正确的命令:-h、-k、-f、-e 或 -d。如果用户没有这样做,那么程序会打印一条错误消息,向主函数返回一个 bool 值零,然后结束程序。如果用户使用其中一个命令,则它返回 1 并且程序在 main 函数中继续。
但是我从编译器那里得到这个错误信息:
comparison between pointer and integer
('bool (*)(char *, char *, int)' and '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;
}
}
最佳答案
这是初学者比较常见的错误:
validate(filename, keyfile, argc);
if (validate == 1) {
// ....
如果你想检查一个函数的返回值,你应该使用实际的函数调用,而不是函数名。像这样:
if (validate(filename, keyfile, argc) == 1) {
// ....
或者,如果您仍想分几步完成:
bool result = validate(filename, keyfile, argc);
if (result == 1) {
// ....
关于c - 由于比较错误,函数未返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48921943/