关于指针学习,留作参考。。。

来源:互联网 发布:ovo是什么意思网络用语 编辑:程序博客网 时间:2024/05/16 11:45

#include <iostream.h>
int *x=new int(4);
int *t=new int(3);
void test(int **a)  
{
    //相当于 a=&t
    cout<<"&a="<<&a<<"  a="<<a<<"     *a="<<*a<<"     **a="<<**a<<endl;
    *a=x;  //或者 **a=*x 
    cout<<"&a="<<&a<<"  a="<<a<<"     *a="<<*a<<"     **a="<<**a<<endl;
    cout<<"&t="<<&t<<"  t="<<t<<"     *t="<<*t<<endl;

}
void test2(int *&a)           //实际上test和test2中a的区别在于 a=&a
{
    //相当于 &a=&t
    cout<<"&a="<<&a<<"  a="<<a<<"     *a="<<*a<<endl;
    a=x;  //或者*a=*x;
    cout<<"&a="<<&a<<"  a="<<a<<"     *a="<<*a<<endl;
    cout<<"&t="<<&t<<"  t="<<t<<"     *t="<<*t<<endl;
   
}
void main()
{
    //对于 int* a,a=*(&a);  *a=**(&a)
    cout<<"&t="<<&t<<"  t="<<t<<"     *t="<<*t<<endl;
    test(&t);
    cout<<"&t="<<&t<<"  t="<<t<<"     *t="<<*t<<endl;
//    test2(t);
//    cout<<"&t="<<&t<<"  t="<<t<<"     *t="<<*t<<endl;
}

//对于指针t,&t为t本身的地址,t为指针t所指向的地址,*t为指针t所指向的地址的内容

//对于函数中t的值的修改,只能是修改t的地址或者是*t的内容,而不能是修改&t的地址,个人觉得是不能修改变量的基址

//只能修改指向地址或地址内容,即基址的下一级