如何摆脱 - “警告:转换为非指针类型“char”;从 NULL”?
我有这样的代码块:
int myFunc( std::string &value )
{
char buffer[fileSize];
....
buffer[bytesRead] = NULL;
value = buffer;
return 0;
}
行 - buffer[bytes] = NULL 给了我一个警告:从 NULL 转换为非指针类型“char”。我该如何摆脱这个警告?
I have this block of code:
int myFunc( std::string &value )
{
char buffer[fileSize];
....
buffer[bytesRead] = NULL;
value = buffer;
return 0;
}
The line - buffer[bytes] = NULL is giving me a warning: converting to non-pointer type 'char' from NULL. How do I get rid of this warning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不使用
NULL
?它通常是为指针保留的,并且您没有指针,只有一个简单的char
。只需使用\0
(空终止符)或简单的0
。Don't use
NULL
? It's generally reserved for pointers, and you don't have a pointer, only a simplechar
. Just use\0
(null-terminator) or a simple0
.buffer[bytesRead] = 0;
// NULL 表示指针作为建议,如果您想避免复制,那么可以考虑下面的方法。
buffer[bytesRead] = 0;
// NULL is meant for pointersAs a suggestion, if you want to avoid copying and all then, below can be considered.
NULL
≠NUL
。NULL
是 C 和 C++ 中表示空指针的常量。NUL
是 ASCII NUL 字符,在 C 和 C++ 中终止字符串并表示为\0
。您还可以使用0
,它与\0
完全相同,因为在 C 中字符文字具有int
类型。 在 C++ 中,字符常量是char
类型。NULL
≠NUL
.NULL
is a constant representing a null pointer in C and C++.NUL
is the ASCII NUL character, which in C and C++ terminates strings and is represented as\0
.You can also useIn C++, character constants are type0
, which is exactly the same as\0
, since in C character literals haveint
type.char
.