#include <iostream>
using namespace std;
void itn(int *b)
{
cout << b[0] << endl;//1
b++;
b[0] = 8;
}
void itnn( int* const & b)//这里必须为常引用,因为会产生一个const类型的临时对象
{
b[0] = 5;
}
void cha(int& x)
{
x++;
}
void chaa(int* st)
{
st++;
*st = 5;//指向一个不存在的地址
}
int main()
{
int a[5] = { 1,2,3,4,5};
itn(a);//指针作为形参传递
for (int i = 0; i < 5; i++)
cout << a[i] << " ";//1 8 3 4 5
cout << endl;
itnn(a);//引用作为形参
for (int i = 0; i < 5; i++)
cout << a[i] << " ";//5 8 3 4 5
cout << endl;
int x = 2;
cha(x);
cout << x<<endl;
chaa(&x);
cout << x;
return 0;
}
指针和引用作为形参的区别
最新推荐文章于 2023-03-04 11:45:47 发布