如何摆脱 - "warning: converting to non-pointer type 'char' from NULL" ?



我有这个代码块:

int myFunc( std::string &value )
{
    char buffer[fileSize];
    ....
    buffer[bytesRead] = NULL;
    value = buffer;
    return 0;
}

行 - buffer[bytes] = NULL 给我一个警告:从 NULL 转换为非指针类型"char"。如何摆脱此警告?

不使用NULL ?它通常是为指针保留的,你没有指针,只有一个简单的char。只需使用(空终止符)或简单的0

buffer[bytesRead] = 0;//NULL 用于指针

作为建议,如果您想避免复制,则可以考虑以下内容。

int myFunc (std::string &value)
{
  s.resize(fileSize);
  char *buffer = const_cast<char*>(s.c_str());
  //...
  value[bytesRead] = 0;
  return 0;
}

NULLNUL .

NULL 是一个常量,表示 C 和 C++ 中的空指针。

NUL是 ASCII NUL 字符,它在 C 和 C++ 中终止字符串并表示为

你也可以使用0,它与完全相同,因为在C中字符文字具有int类型。在C++中,字符常量的类型为 char

相关内容