gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:14:55 24 4
gpt4 key购买 nike

我正在尝试编写一个可以处理重定向的简单 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com