关于数组问题

永远的明日 2009-03-12 01:15:13
下面的程序中,一个是字符数组,一个是整型数组,为什么输出的结果一个是数组的内容,另一个是首元素的地址?恳请请各位大大赐教!要是我想输出字符数组首元素的地址该怎么做?

#include<iostream>
using namespace std;

int main()
{
char a[10] = "abcdefghi";
int b[2] = {1,2};
cout<<a<<endl;
cout<<b<<endl;
return 0;

}
...全文
85 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
夹心饼干 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 rabbitlzx 的回复:]
引用 4 楼 stormlk1983 的回复:
cout < <a < <endl---输出整个字符串
cout < <b < <endl;---输出的是b的首地址

要是我想输出字符数组首元素的地址该怎么做? ------cout < <a[0];
or cout < <*a;



这个输出的是第一个元素的值吧
[/Quote]看错了,我输出的是首元素的值
chin_chen 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hairetz 的回复:]
C/C++ code#include<iostream>usingnamespacestd;intmain()
{chara[10]="abcdefghi";intb[2]={1,2};
cout<<&a<<""<<&a[0]<<endl;//这样都可以输出第一个元素的地址cout<<a<<endl;
cout<<b<<endl;return0;

}
[/Quote]
cout<<&a[0]<<endl;这样不可以!
你最好是做作测试再发上来吧,不然误人子弟啊!
永远的明日 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hairetz 的回复:]
C/C++ code
#include<iostream>
using namespace std;

int main()
{
char a[10] = "abcdefghi";
int b[2] = {1,2};
cout << &a << " " << &a[0] <<endl; //这样都可以输出第一个元素的地址
cout<<a<<endl;
cout<<b<<endl;
return 0;

}
[/Quote]
呵呵,&a[0]还是会输出整个字符串··不过还是谢谢各位了·
  • 打赏
  • 举报
回复

#include<iostream>
using namespace std;

int main()
{
char a[10] = "abcdefghi";
int b[2] = {1,2};
cout << &a << " " << &a[0] <<endl; //这样都可以输出第一个元素的地址
cout<<a<<endl;
cout<<b<<endl;
return 0;

}
chin_chen 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 gyjudrnrso 的回复:]
哦,我打错了,我是想问在如下程序中,如何输出p的内容
C/C++ code#include<iostream>usingnamespacestd;intmain()
{chara[10]="abcdefghi";intb[2]={1,2};char*p=a;
cout<<p<<endl;
cout<<b<<endl;return0;

}
[/Quote]
#include<iostream> 
#include <cstdio>
using namespace std;

int main()
{
char a[] = "abcdefghi";
int b[2] = {1,2};

char*p=a;

cout<< (int*)p<<endl;//改一下啦。
cout<<b<<endl;
system("pause");
return 0;

}
chin_chen 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 gyjudrnrso 的回复:]
引用 5 楼 AlwaysSLH 的回复:
C/C++ code
cout < <&a < <endl;
cout < <(void*)a < <endl;
cout < <static_cast <void*>(a) < <endl;

为什么要转换类型呢?
[/Quote]
不一定非要这样了,随便你一个cout<<(int*)a<<endl;也可以的。原理一样的。


这个跟cout < <这个操作符有关,它把任何一个char*都当成一个字符串来输出,而int*或是其它的,它就当地址来输出了。
永远的明日 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 AlwaysSLH 的回复:]
C/C++ code
cout<<&a<<endl;
cout<<(void*)a<<endl;
cout<<static_cast<void*>(a)<<endl;
[/Quote]
为什么要转换类型呢?
rabbitlzx 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 stormlk1983 的回复:]
cout < <a < <endl---输出整个字符串
    cout < <b < <endl;---输出的是b的首地址

要是我想输出字符数组首元素的地址该怎么做? ------cout < <a[0];
                                      or    cout < <*a;

[/Quote]

这个输出的是第一个元素的值吧
永远的明日 2009-03-12
  • 打赏
  • 举报
回复
哦,我打错了,我是想问在如下程序中,如何输出p的内容

#include<iostream>
using namespace std;

int main()
{
char a[10] = "abcdefghi";
int b[2] = {1,2};
char *p = a;
cout<<p<<endl;
cout<<b<<endl;
return 0;

}
AlwaysSLH 2009-03-12
  • 打赏
  • 举报
回复

cout<<&a<<endl;
cout<<(void*)a<<endl;
cout<<static_cast<void*>(a)<<endl;
夹心饼干 2009-03-12
  • 打赏
  • 举报
回复
cout<<a<<endl---输出整个字符串
cout<<b<<endl;---输出的是b的首地址

要是我想输出字符数组首元素的地址该怎么做? ------cout<<a[0];
or cout<<*a;
chin_chen 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用楼主 gyjudrnrso 的帖子:]
下面的程序中,一个是字符数组,一个是整型数组,为什么输出的结果一个是数组的内容,另一个是首元素的地址?恳请请各位大大赐教!要是我想输出字符数组首元素的地址该怎么做?

[/Quote]
#include<iostream> 
using namespace std;

int main()
{
char a[10] = "abcdefghi";
int b[2] = {1,2};
cout<< &a<<endl;//改下就可以了
cout<<b<<endl;
system("pause");
return 0;

}
pengzhixi 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chin_chen 的回复:]
这个跟cout < <这个操作符有关,它把任何一个char*都当成一个字符串来输出,而int*它就当地址来输出了。
[/Quote]

up
chin_chen 2009-03-12
  • 打赏
  • 举报
回复
这个跟cout<<这个操作符有关,它把任何一个char*都当成一个字符串来输出,而int*它就当地址来输出了。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧