以下面的代码为例:
int main(){
char string[] = { 'h' , 'e', 'l', 'l', 'o' };
printf(string);
printf("\n%d", strlen(string));
}
输出将是:
hello
6
所以一开始我就可以看到 strlen
值减少了 1,但如果我们考虑到它,它似乎并不是一个障碍。
什么时候不终止字符串会产生问题?
最佳答案
What are the repercussions of not using null termination in strings?
技术上没有,因为 C 字符串是 - by definition - 以空字符终止:
A string is a contiguous sequence of characters terminated by and including the first null character.
所以如果它没有正确终止,它就不是一个字符串。
如果将非字符串传递给需要字符串的代码,则会调用未定义的行为。
关于c - 在字符串中不使用空终止会产生什么影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52720975/