指针学习

来源:互联网 发布:excel2007软件电脑版 编辑:程序博客网 时间:2024/06/10 06:07
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;
}
  

原创粉丝点击