1.C 中指针和引用的物理实现是一回事,都是内存地址;两者的区别是在编译时编译器无法对指针操作进行类型检查,而对引用可以。这也是引用更安全的原因;
2.指针实例1:
void compair(int *m,int *n )
{
if (*m>*n)
{
int t;
t=*m;
*m=*n;
*n=t;
}
}
//error
void compaire(int m,int n )
{
if (m>n)
{
int t;
t=m;
m=n;
n=t;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//simple
int m,n;
cout<<"input m:"<<endl;
cin>>m;
cout<<"input n:"<<endl;
cin>>n;
compaire(m,n);
cout<<m<<","<<n<<endl;
compair(&m,&n);
cout<<m<<","<<n<<endl;
return 0;
}
3.指针实例2:
void point1(int *t)
{
int p=15;
*t = 25;
t=&p;
}
int _tmain(int argc, _TCHAR* argv[])
{
int *s;
int m=5;
s = &m;// 比较*s=m;
point1(s);
cout<<*s<<endl;
return 0;
}
2.指针实例1:
void compair(int *m,int *n )
{
if (*m>*n)
{
int t;
t=*m;
*m=*n;
*n=t;
}
}
//error
void compaire(int m,int n )
{
if (m>n)
{
int t;
t=m;
m=n;
n=t;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//simple
int m,n;
cout<<"input m:"<<endl;
cin>>m;
cout<<"input n:"<<endl;
cin>>n;
compaire(m,n);
cout<<m<<","<<n<<endl;
compair(&m,&n);
cout<<m<<","<<n<<endl;
return 0;
}
3.指针实例2:
void point1(int *t)
{
int p=15;
*t = 25;
t=&p;
}
int _tmain(int argc, _TCHAR* argv[])
{
int *s;
int m=5;
s = &m;// 比较*s=m;
point1(s);
cout<<*s<<endl;
return 0;
}
本文深入探讨了C语言中的指针与引用的物理实现与区别,通过实例展示了指针的操作方法,并对比了引用的安全优势。具体包括指针的定义、使用、错误案例分析以及引用的概念、安全特性和实际应用。
16万+

被折叠的 条评论
为什么被折叠?



