【发布时间】:2011-10-07 13:46:26
【问题描述】:
我已经测试了两个不同的相似代码变体,假设我有 char 数组
char x[]="snow comes in winter ";
然后是下面的代码
#include <iostream>
#include <string.h>
using namespace std;
int main(){
char x[]="snow comes in winter ";
int k=0;
int n=3;
cout<<x+n-k+1<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
打印"comes in winter "
在关注时
#include <iostream>
#include <string.h>
using namespace std;
int main(){
//char x[]="snow comes in winter ";
int a[]={12,3,2,4,5,6,7};
int k=0;
int n=3;
cout<<a+n-k+1<<endl;
return 0;
}
打印0xbffd293c
如果我们稍微改变一下
#include <iostream>
#include <string.h>
using namespace std;
int main(){
//char x[]="snow comes in winter ";
int a[]={12,3,2,4,5,6,7};
int k=0;
int n=3;
cout<<*(a+n-k+1)<<endl;
return 0;
}
打印数字 5。 所以我的问题是为什么我们可以如此轻松地访问 char 数组?它的主要原因是什么?我很好奇,请有人解释一下吗?
【问题讨论】:
-
你能澄清你的问题吗?
-
好的,为什么它会针对不同类型的数组显示不同的结果?(在这种情况下,给出了 char 和整数数组)