又一个简单C++问题

来源:百度知道 编辑:UC知道 时间:2024/05/29 02:42:58
#include<iostream>
using namespace std;

int main()
{
char our_string[5];

int index=0;

while(index<5)
{
our_string[index]='X';
index++;
}
cout<<our_string;
return 0;
}

我这程序 字符串数组下标没越借啊 程序打印了五个X但是为什么后面还有乱码啊
char p[3]="sss";
请问一下像这种他不都是自动加的结束符号吗

字符串数组有个特点就是必须已'\0'结束
你把所有位置都放上字母了,应该把最后一个赋值为'\0'
//
char p[3]="sss";是错误的
应该是char p[]="sss";

没有加结束符,赋4个值最后一个赋'\0'或者0.