指针与整数之间的警告比较 [英] Warning comparison between pointer and integer

查看:172
本文介绍了指针与整数之间的警告比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我遍历字符指针并检查指针何时到达空终止符时,我会收到警告.

I am getting a warning when I iterate through the character pointer and check when the pointer reaches the null terminator.

 const char* message = "hi";

 //I then loop through the message and I get an error in the below if statement.

 if (*message == "\0") {
  ...//do something
 }

我得到的错误是:

warning: comparison between pointer and integer
      ('int' and 'char *')

我认为message前面的*取消了对消息的引用,所以我得到了message指向何处的值?我不想顺便使用库函数strcmp.

I thought that the * in front of message dereferences message, so I get the value of where message points to? I do not want to use the library function strcmp by the way.

推荐答案

应该是

if (*message == '\0')

在C语言中,单引号分隔单个字符,而双引号分隔字符串.

In C, simple quotes delimit a single character whereas double quotes are for strings.

这篇关于指针与整数之间的警告比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆