c - 指针和整数之间的警告比较

标签 c null-terminated

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

 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 前面的 * 取消引用消息,所以我得到消息指向哪里的值?顺便说一句,我不想​​使用库函数strcmp

最佳答案

应该是

if (*message == '\0')

在 C 中,单引号分隔单个字符,而双引号用于字符串。

关于c - 指针和整数之间的警告比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32510218/

相关文章:

c++ - NULL 终止 c_str()?

c++ - 什么是空终止字符串?

c - 在 C 中打印字符串时出现无法解释的行为

c - 关于使用 c 进行汇编

c++ - C++ 中方法返回的编码约定

c - 函数average 和stdDev const int tab[ ]。一般问题

c - 有效地转置一个大的(密集的)二进制矩阵

c++ - 将 strncpy 放入一个没有空终止符空间的字符串中是否安全?

c - NULL是如何终止的?

c - 带有字符串参数的 fprintf