指针初步

void main()
{
    int ival = 42;
    int *p = &ival;
    cout << "p: "<< p<<"\n"
         << "*p: "<<*p;
}

// p:0015FE28    访问变量ival的地址,或者说p是指向变量ival的指针
// *p:42  *为解引用符,访问ival的对象

void main()
{
    int ival = 42;
    int *p = &ival;
    *p = 0;
    cout << "p: " << p << "\n" 
          << "*p: " << *p<<"\n"
          <<"ival: "<

下面是引用和指针的问题:

void main()
{
    int ival = 42;
    int &t = ival;  // t为引用,在修改了t=0之后,p也进行了修改
    int *p;
    p = &t;
    cout << "p: " << p << "\n" << "*p: " << *p << endl;
    cout << "ival: " << ival << "\n" << "&ival: " << &ival << endl;
    t = 0;
    //*p = 0;
    cout << "p: " << p << "\n" << "*p: " << *p << endl;
    cout << "ival: " << ival << "\n" << "&ival: " << &ival << endl;
}

/*
p: 0028F93C
*p: 42
ival: 42
&ival: 0028F93C

p: 0028F93C
*p: 0
ival: 0
&ival: 0028F93C
*/



void main()
{
    int ival = 42;
    int *t = &ival;  // t为指针指向ival的地址,在t修改了后,p的指针仍指向ival的地址
    int *p;
    p = t;
    cout << "p: " << p << "\n" << "*p: " << *p << endl;
    cout << "ival: " << ival << "\n" << "&ival: " << &ival << endl;
    t = 0;
    //*p = 0;
    cout << "p: " << p << "\n" << "*p: " << *p << endl;
    cout << "ival: " << ival << "\n" << "&ival: " << &ival << endl;
}

/*
p: 0037FBAC
*p: 42
ival: 42
&ival: 0037FBAC
p: 0037FBAC
*p: 42
ival: 42
&ival: 0037FBAC
*/

你可能感兴趣的:(指针初步)