In-depth analysis of the stack pointer

Why is the value of printing root the same as the value of &root->value?

 Test Results:

What exactly is the variable *?

I wrote a sentence before, that is to say, if you see a * variable, it is the memory address stored in this variable, and then take out the corresponding value or address stored in it.

So how to understand this sentence, it can be said like this

Then the results printed above are the same, and the addresses of &num1 are taken out

The specific code description is as follows:

#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{
    int num1 = 3;
    int *p_num1 = &num1;
    int **pp_num1 = &p_num1;
    int ***ppp_num1 = &pp_num1;

    printf("%d\n",*p_num1);//直逼num1,所以打出来是3

    printf("%d  %d\n",*pp_num1,&num1);//直逼p_num1,取出来就是num1的地址

    printf("%d  %d\n",*ppp_num1,&p_num1);//直逼pp_num1,取出来p_num1的地址

    //第一个*号直逼pp_num1,取出&p_num1地址
    //第二个*号直逼p_num1,取出&num1的地址
    printf("%d  %d\n",**ppp_num1,&num1);
    
    //这里有两个**,一个*直逼pp_num1保存的地址就是p_num1,取出来是
    //&num1的地址,然后再来一个*,直逼num1的地址,取出来的就是3
    printf("%d\n",**pp_num1);

    return 0;
}

operation result:

,&(*root)->left Why does the expression report an error

Before talking about this, let's analyze the values ​​​​of the following expressions

表达式1:printf("%d %d %d %d\n",&(*root),root,&root->value,&root);

 Expression 2: printf("%d %d\n",&(*pp_root)->left,&root->left);

表达式3: printf("%d %d %d\n",(*pp_root)->left,root->left,&child->value);

Let me talk about why &(*root)->left reported an error

first look at a picture

Let’s analyze the above. First, *root returns a node object itself without a pointer reference, so the ->left compiler will report an error.

Since the node object itself is returned, it can be referenced with ., as the following two expressions have the same value

 That's why it reports an error

Binary tree memory node analysis

 

When your * number is close to an address allocated on the heap, what is returned 

 

So what does this *p_root return, as I said before, if the * number is a variable, then it is directly equal to the value of the memory address saved by this variable, either an address or a value.

Here *p_root is obviously the situation pointed by the blue arrow below

 This will take out an address without reporting an error, but this address actually has no practical meaning. It can be understood in this way that it is the node object returned on the heap.

Since it is an object, we can use . (dot to access the members inside)

upper code

#include <cstdio>
#include <cstdlib>

struct node 
{
    int value;
    node *next;
};



int main()
{

    node *p_root = new node;
    node *p_next = new node;

    p_root->next = p_next;
    p_root->value = 520;

    p_next->value = 666;
    p_next->next = NULL;


    printf("%d  %d\n",p_root,*p_root);//前者是p_root在堆上的地址,后者是它在堆上引用

    //引用返回一个node对象,而不是一个指针,就用.号去访问成员
    printf("%d  %d\n",(*p_root).value,(*p_root).next);//这里注意运算符优先级顺序,点.的优先级绝对高
    /**
     *value是520,然后地址打印的是p_next指向的在堆上的地址 
     */

    return 0;
}

operation result

 

 Well, good morning, good afternoon, good night. 

 

Guess you like

Origin blog.csdn.net/Pxx520Tangtian/article/details/132232652