SL.str.4:使用char*参照单独字符
The variety of uses of char* in current code is a major source of errors.
现存代码中关于char*的各种用法是错误的主要来源之一。
Example, bad(反面示例)
char arr[] = {'a', 'b', 'c'};
void print(const char* p)
{
cout << p << '\n';
}
void use()
{
print(arr); // run-time error; potentially very bad
}
The array arr is not a C-style string because it is not zero-terminated.
由于不是以0结尾,因此代码中的arr数组并不是C风格字符串。
Alternative(可选项)
See zstring, string, and string_view.
参见zstring, string, and string_view.
Enforcement(实施建议)
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#slstr4-use-char-to-refer-to-a-single-character