address-of和指针地址有什么区别?

时间:2014-10-26 08:01:14

标签: c++

普通指针地址和指针“地址”有什么区别?

#include<iostream>

using namespace std;

int main()
{
    int * a= new int[1];
    cout<<a<<endl;
    cout<<&a<<endl;
    return 0;
}

这会打印两个不同的值:

0x8b1a438
0xffbb6b8c

3 个答案:

答案 0 :(得分:5)

图片通常价值千言万语:

           +------------+
0x08b1a438 |         42 | int[1]
           +------------+
                  ^
                  |
                  |
           +------|-----+
0xffbb6b8c | 0x08b1a438 | int * a
           +------------+

如您所见,打印a打印a的内容,打印&a打印其地址。

答案 1 :(得分:1)

这会打印出a的值,即它指向的地址:

cout << a <<endl;

这会打印出a本身的地址:

cout<< &a <<endl;

这类似于其他内置类型的情况,例如

int b = 42;
cout << b << endl;  // prints the value of b, i.e. 42
cout << &b << endl; // prints the address of b

指针的值恰好是另一个对象的地址。 ab的值都可以更改。他们的地址不能。

a = &b; // modify value of a: now it points to b.
b = 99; // modify the value of b: now it is 99.

答案 2 :(得分:0)

a是一个指针变量,包含整数的地址。

&amp; a是指针变量本身的地址,即它是保存整数地址的地址。