#include#includeusingnamespacestd;intmain(){inti,j;chars[3][8];scanf("%s",s[0]);strcpy(s[1],"s1");s[2][0]='s';s[2][1]='1';s[2][2]='\0';//输出字符串数组s...
#include
#include
using namespace std;
int main()
{
int i,j;
char s[3][8];
scanf("%s",s[0]);
strcpy(s[1],"s1");
s[2][0]='s';
s[2][1]='1';
s[2][2]='\0';
//输出字符串数组s
for(i=0;i<3;i++)//不论怎么限制都会s字符串数组访问越界??????????
{
for(j=0;j<8;j++)
{
cout << s[i][j] ;
}
cout << endl;
}
char name[3][8]={"mary","smith","goodbye"};//但是这个就显示正确,这是为什么?????????除了初始化,别的地方都一样啊,麻烦各位帮忙看看,谢谢。
for(i=0;i<3;i++)
{
for(j=0;j<8;j++)
{
cout << name[i][j];
}
cout << endl;
}
cout << endl << endl;
return 0;
}
这样运行结束的结果显示:
s1
s1 烫烫
s1 烫烫
s1 烫烫
mary
smith
goodbye
press any key to continue_
后来我又把上面的第一个for循环代码更改了一下
for(i=0;i<3;i++)//还是会s字符串数组访问越界?????什么原因啊????
{
for(j=0;j<8;j++)
{
cout << s[i][j] ;
}
if(s[i][j]=='\0')
{
cout << endl;
continue;
}
}
这回运行的结果更奇怪了,结果显示如下:
s1
s1 烫烫蘳1 烫烫蘳1 烫烫
mary
smith
goodbye
press any key to continue_
展开