我有一个功能:
// string is a null-terminated char array. Replace all a in the string with b
void ReplaceCharInString(char *string, char a, char b)
{
// loop over the string char by char, to find all "a"s and replace them with "b"
}
Run Code Online (Sandbox Code Playgroud)
我在做防守编程.问题是客户端上的实现回复真正传递了一系列字符.如果传入单个字符的地址,程序肯定会遇到错误的状态(可能崩溃).我该如何检查并避免这种情况?(我知道如果我传入std :: string对象,问题就会消失)