不显示字符串源
strcpy source is not displayed
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main ()
{
char a[]="one string",b[]="twostrings";
strcpy (a,b);
cout<<"A="<<a;
cout<<endl<<"B="<<b<<endl;
}
a和b在显示后是相等的,但是如果我像这样加一个空格b[]="two strings"
,然后再加上b,b显示为空白,为什么?
因为有了空间,a[]
缓冲区不足以容纳b
的副本,并且您试图使它这样做会损坏堆栈,从而产生未定义的行为。任何事情都可能发生,但在您的情况下,很可能是用终止的NUL
覆盖下一个变量(即b
)的第一个字节,使b
显示为空。
拼出来:
a b
one string0two strings0 // original content 0=NUL
two strings0wo strings0 // after copy
必须提示:尽可能使用std::string
相关文章:
- 字符串-C++后显示的随机字符
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 显示字符串的集合和子集
- 在将字符插入 2d 字符串时如何不显示空字符?
- 两个数组,int 和 string,如何显示带有对应 int 的字符串?
- 为什么我的 cout 在输出中没有显示字符串?
- 为什么在为其分配编号后无法显示我的字符串值?
- 分段故障 11,从类函数显示动态 C 字符串
- 在大小为 10 的 char 数组中输入超过 10 个字符的字符串并输出时,它会显示整个数组而不是前 10 个数组.为什
- isPalindrome不显示输出,isPalindrome函数未使用字符串输入作为字符串参数进行测试
- 输出显示多个带整数的字符串值
- 字符串显示上的智能感知:npos = 18446744
- 如何从显示板将字符显示为字符串?
- 插入了 C++ 数组字符串数据,但在显示输出时不显示
- 使用宏显示宏的字符串化内容
- 我将如何将数组的显示从数字更改为字符串C++
- 如何让我的构造函数和函数工作,以便我的 main() 能够同时显示字符串和 int 数据
- C 我将如何在字符串中显示字符作为数字
- 如何使用另一个字符串显示三个浮点数的最大值?
- 如何比较"4A4B4C"(字符串显示十六进制值)实际字符串"JKL"