gpt4 book ai didi

C++ strncpy 意外输出

转载 作者:太空宇宙 更新时间:2023-11-04 15:00:16 24 4
gpt4 key购买 nike

我正在审查函数 strncpy 的字符串操作函数。我有 7 个输出而不是 3 个,谁能解释一下?非常感谢。

    char x[] = "just see that.";
char y[15];
char z[10];
char l[14];

strcpy(y, x);

cout << "The string in array x is: " << x
<< "\n The string in array y is: " << y << '\n';

strncpy(z, x, 9);
z[9] = '\n';

cout << "The string in array z is: " << z << endl;

strncpy(l, x, 13);
l[13] = '\n';

cout << "The string in array l is: " << l<< endl;

输出有7行,如下图

The string in array x is: just see that.
The string in array y is: just see that.
The string in array z is: just see
just see that.
The string in array l is: just see that
just see
just see that.

最佳答案

如果你想空终止,你应该使用'\0'而不是'\n'

关于C++ strncpy 意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49952576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com