gpt4 book ai didi

c++ - 编译c++代码时"Error: comparison between pointer and integer"

转载 作者:行者123 更新时间:2023-11-28 01:38:47 25 4
gpt4 key购买 nike

在C++(编译器:clang++)中,当编译如下代码时:

char* strcpy(char * dest, const char * src)
{
char* result = dest;
if(('\0' != dest) && ('\0' != src))
{
/* Start copy src to dest */
while ('\0' != *src)
{
*dest++ = *src++;
}
/* put '\0' termination */
*dest = '\0';
}
return result;

}

我收到以下错误代码:

string/strcpy.cpp:12:11: error: comparison between pointer and integer
('int' and 'char *')
if(('\0' != dest) && ('\0' != src))
~~~~ ^ ~~~~
string/strcpy.cpp:12:29: error: comparison between pointer and integer ('int'
and 'const char *')
if(('\0' != dest) && ('\0' != src))

我知道与此错误相关的大多数错误都是在要比较的字符位于引号而不是撇号之间时产生的,但在此代码中情况并非如此。为什么会产生这个错误?提前致谢!

最佳答案

你是说

if((NULL != dest) && (NULL != src))

更加地道

if((dest) && (src))

关于c++ - 编译c++代码时"Error: comparison between pointer and integer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48232445/

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