When learning data structures, creating linked lists, he defined a second level pointer, which was very confusing, hence this blog post. What’s the use of the pointer *? In my opinion, it’s like matching the opponent with the rank. Like if I’m Black Iron 4, I can’t match the best.

Understand the variables you define and how they came about:

Int a = 100 int a = 100 int a = 100 int a = 100

Int *p1 = &a, first we need to create a space in memory, and then if the value of p1 is 0X62, then notice that we also assigned the value of p1, so it points to the address of A: 0X61.

Int **p2 = &p1; if **p2 is 0X63, it points to the address of p1.

The diagram below:

To sum up:

In fact, every variable (whether pointer variable or primitive data type) should have two values, one is its address value, one is stored (stored) value, but the primitive type and the pointer is not the same, one holds the data, the other holds the address.

For int A, its address is 0X61 and its address is 100. For int * P1, its address is 0X62 and its address is 0X61. For int **p2, its address is 0X63 and its address is 0X62.

Time for Conan:

Now to reveal what the * in front of the pointer does (the best evidence to know what this thing does is to print the result in code) :

#include <stdio.h>
int main(a){
    int a =100;
    int *p1 = &a;
    int **p2 = &p1;
    int ***p3 = &p2;
    printf("%d, %d, %d, %d\n", a, *p1, **p2, ***p3);
    printf("&p2 = %#X, p3 = %#X\n", &p2, p3);
    printf("&p1 = %#X, p2 = %#X, *p3 = %#X\n", &p1, p2, *p3);
    printf(" &a = %#X, p1 = %#X, *p2 = %#X, **p3 = %#X\n", &a, p1, *p2, **p3);
    return 0;
}
Copy the code
100.100.100.100
&p2 = 0X28FF3C, p3 = 0X28FF3C
&p1 = 0X28FF40, p2 = 0X28FF40, *p3 = 0X28FF40
&a = 0X28FF44, p1 = 0X28FF44, *p2 = 0X28FF44, **p3 = 0X28FF44
Copy the code

So let’s just refine it a little bit from the result, and then from the book, ampersand is the address symbol for fetching variables, and star is the value symbol.

  1. Want to getThe data to which the pointer points, the first level pointer is added by one*, secondary pointer plusTwo *, three pointer plusThree *And so on.
  2. And if I lose one pointer*, means that the pointer points to the address value of the data.
  3. And the pointer doesn’t add*“, which indicates what value he has stored.

Put this together:

My second level pointer, with two *, can directly refer to the data of a, and the missing * represents the address value pointing to A (that is, the value stored in the point p1). That again little one can only refer to oneself, that is not oneself store p1 address value.

From this, we can see that the * of the pointer is like a rank. The number of * points to the value of which segment matches which segment.

Well, that’s what Pointers are, isn’t it? Ha ha, just kidding.

Recommended reading: c.biancheng.net/view/2016.h…