关于指针的一个疑问

piaomiao163 2009-02-16 04:46:59
请看下面代码:

#include <iostream>
using namespace std;
template < typename T >
void outputArray( const T * P_array, const int count )
{
for( int i = 0; i < count; i ++ )
cout << P_array[ i ] << "";
cout << endl;
}

int main()
{
const int aCount = 8, bCount = 8, cCount = 20;
int aArray[ aCount ] = { 1,2,3,4,5,6,7,8 };
double bArray[ bCount ] = { 1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8 };

char cArray[ cCount ] = "Welcome to see you!";
cout << "a Array contains:" << endl;
outputArray( aArray, aCount );
cout << "b Array contains:" << endl;
outputArray( bArray, bCount );
cout << "cArray,cCount:" << endl;
outputArray( cArray, cCount );
system("pause");
}


void outputArray( const T * P_array, const int count )声明的时候第一个参数声明的是一个指针的形式,为什么在调用的时候此参数接受的并不是指针形式的而是outputArray( aArray, aCount );

...全文
124 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ronal_Lee 2009-02-16
  • 打赏
  • 举报
回复
当传入参数是数组名时,就会退化为一个相应类型的指针
叶落寒山 2009-02-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tangshuiling 的回复:]
C/C++ code
数组在进行函数参数传递时退化为指针!
[/Quote]

同意
operatingtuzi 2009-02-16
  • 打赏
  • 举报
回复
表达式和参数传递时数组名隐式转化为指针
你可以couta<<Array<<&aArray[0]
他们是一样的
go_go_vip 2009-02-16
  • 打赏
  • 举报
回复
在调用数组时要用的是指针,再用指针的形式就相当于指针的指针了
changjiangcug 2009-02-16
  • 打赏
  • 举报
回复
某些时候你可以简单的把数组看做指针,虽然存在差别。
waizqfor 2009-02-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 piaomiao163 的帖子:]
请看下面代码:

C/C++ code
#include <iostream>
using namespace std;
template < typename T >
void outputArray( const T * P_array, const int count )
{
for( int i = 0; i < count; i ++ )
cout << P_array[ i ] << "";
cout << endl;
}

int main()
{
const int aCount = 8, bCount = 8, cCount = 20;
int aArray[ aCount ] = { 1,2,3,4,5,6,7,8 };
double bArray[ bCoun…
[/Quote]
比如 char []和char *是等价的 所以传递是没有问题的
还有就是数组做为形参的时候 会退化为指针的类型!~
a_rockboy 2009-02-16
  • 打赏
  • 举报
回复
C++语言里数组基本等同于只读指针。
tangshuiling 2009-02-16
  • 打赏
  • 举报
回复

数组在进行函数参数传递时退化为指针!

65,211

社区成员

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

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