指针变量,var和& var的差异

发布于 2025-02-10 09:22:29 字数 707 浏览 1 评论 0原文

#include <iostream>

using namespace std;

int main() {
    int* z = new int(9);

    cout << "address: " << z << endl;
    cout << "value: " << *z << endl;
    cout << "referance: " << &z << endl;
    return 0;
}

看着cout值,我期望地址和参考会提供相同的地址,但是Heres输出是什么:

address: 0x7fc452c032a0
value: 9
referance: 0x7fff5191b8d8

只是对此的原因感到好奇,是普通的值(z),堆在堆中的地址值为9,其中var(&amp; z)是位于堆栈中的指针变量的地址?

这是一个可视化:

“在这里输入图像描述”

#include <iostream>

using namespace std;

int main() {
    int* z = new int(9);

    cout << "address: " << z << endl;
    cout << "value: " << *z << endl;
    cout << "referance: " << &z << endl;
    return 0;
}

Looking at the cout values, I was expecting the address and reference to give the same address, but heres what the output is:

address: 0x7fc452c032a0
value: 9
referance: 0x7fff5191b8d8

Just curious about the reason for this, is the plain value(z) the address of the variable in the heap with a value of 9, where var(&z) is address of the pointer variable which is located in the stack?

Here is a visualization:

enter image description here

Is the

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长途伴 2025-02-17 09:22:29

&amp; z指定 Adress 指针int * z在其中存储分配的adress new Int(9)

指针z和值9存储在内存中的两个不同位置。

这里没有任何参考的概念,只有 adresses

&z designates the adress of the pointer int * z where you store the allocated adress new int(9).

The pointer z and the value 9 are stored at two different locations in memory.

There is not any notion of reference here, only adresses.

半透明的墙 2025-02-17 09:22:29

让我首先浏览一些基础知识。

  • 一个变量是一个名称,用于指代内存中的某些位置,该位置具有我们正在使用的值。

  • 使用'&amp;' 获得变量的地址。

  • 指针是存储变量地址的变量。例如,在示例中,您指的是

      int* z = new int(9);
     

    变量z存储值9 [new Int(9)]的地址。

现在,最终必须将此变量存储在内存中的某个位置,并且可以使用ampersand(&amp;)访问。

    &z //gives the address of the pointer to value 9 (address of variable z).

这与指针和指针指针(多级指针)的作用相同。

Let me go through some of the basics first.

  • A variable is a name that is used to refer to some location in the memory, a location that holds a value with which we are working.

  • Using '&' in C/C++ we can get the address of the variable.

  • A pointer is a variable that stores the address of a variable. For instance, in the example you are referring to

    int* z = new int(9);
    

    variable z stores the address of the value 9 [new int(9)].

Now, finally this variable has to be stored at some location in the memory and this can be accessed using ampersand (&).

    &z //gives the address of the pointer to value 9 (address of variable z).

This is the same way the pointers and pointers to a pointer (multi level pointers) works.

十雾 2025-02-17 09:22:29
int* z;

上面的语句意味着int类型声明的指针变量。

int* z = new int();

上面的语句意味着一个地址分配给int动态类型的指针变量。

int* z = new int(9);

上面的语句意味着值9存储在动态分配中。

cout << "address: " << z << endl;

上面的代码行告诉指针变量的地址z

cout << "value: " << *z << endl;

上面的代码行告诉可变z中存储的值。

cout << "referance: " << &z << endl;

上面的代码行告诉动态创建的变量地址。

int* z;

Above statement implies a pointer variable of int type declaration.

int* z = new int();

Above statement implies an address is allocated to pointer variable of int type dynamically.

int* z = new int(9);

Above statement implies value 9 is stored in a dynamically allocated.

cout << "address: " << z << endl;

Above line of code tells the address of pointer variable z.

cout << "value: " << *z << endl;

Above line of code tells the value stored in the variable z.

cout << "referance: " << &z << endl;

Above line of code tells the dynamically created variable's address.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文