c - 错误: comparison between a pointer and an integer

标签 c

我正在尝试编写一个可以处理重定向的简单 shell。但是,我得到“错误:指针和整数之间的比较”。我明白为什么会出现错误,但我不确定如何修复它。 args 声明为:

static char* args[512];

这是我收到错误的代码:

if(args == '<'){
     int fd0 = open("input.txt", READ, 0);
     dup2(fd0, STDIN_FILENO);
     close(fd0);
}
if(args == '>'){
     int fd1 = creat("output.txt", 0644);
     dup2(fd1, STDOUT_FILENO);
     close(fd1);
}

错误出现在 if(args == '<') 和 if(args == '>') 行

欢迎任何建议。

最佳答案

我假设 args 是一个指针 char (char *)。 如果是这样,则您正在将指针与单个字符进行比较。 你可以这样做:

if(args[0] == '>')
//then do something

关于c - 错误: comparison between a pointer and an integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23352578/

相关文章:

c - 指向结构体的指针中的元素数量

c - assembly - 来自堆栈的编号

c - 在 C 中使用 pthread?

在 GCC 中调用程序集?

c - Bison解析全局变量和函数

c - 当我在嵌套的 for 循环中包含括号时,它无法正常运行,但当我将它们取出时,它可以正常运行;为什么是这样?

c - C中的红黑树实现

c - 如何获取标准输入的第一个字符并丢弃其余字符?

c - PTHREAD_COND_INITIALIZER - 函数 pthread_cond_wait() 分支到慢速路径

编译错误: expected expression before ‘{’ token