我可以确定char*参数解决了多少有效内存?

Eri*_*c Z 4 c c++

我有一个功能:

// 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对象,问题就会消失)

sth*_*sth 12

不,你不能检查这一点,并且必须信任该函数的用户传递一个实际正确的以null结尾的字符串.

这也是像这样所有的C标准库函数strcpy(),strlen()...的工作.