C language ----- pointer

  A pointer is a variable whose value is a memory address. The core of a pointer is that it is a variable, but it is used to store the memory address. Before understanding pointers, let's talk about what variables are. A variable is a space created in memory. Such as int year, is to open up a space in the memory, the name of the space is year, what is the purpose of opening up the space? It must be to store what the program needs to run. In computer languages, storing things is assignment. year= 2018, assigning a value to year is to store an integer 2018 in the space of year; the space is opened up, and the value is also stored, so what should we do if we want to use the things in this space? How can we find this space? Of course, the easiest way is to use the name year directly. In fact, there is another way to find the address of this space. After finding the address, we can do anything. When memory opens up space, it will automatically have an address, and this address is the memory address, which is the same as our daily life. For example, if a developer takes a piece of land, it is equivalent to opening up space. When the land is acquired, that is, when the space is opened, it will automatically have an address. The land he takes in Guangdong Province must be located in Guangdong Province, not Beijing, and the address will not change. It's just that the address is described differently. In real life, it is Shenzhen, Guangdong Province, while in the computer, it is a hexadecimal number, oXc522;

  Every variable has an address. If we want to operate this address, we need to get the address and save it. To save something is to use a variable. This variable stores the memory address, so the name is a pointer. The declaration of the pointer variable is also different, with * declaration. int *point_year. How to get the variable address Use & &year to get the memory address of year, then it can assign the group point_year variable. point_year = &year, point_year stores the memory address of year, so it can also be said that point_year points to year. There is a special constant here, NULL, which means null, such as point_year = NULL, which means that the pointer does not point to any address .

When * is used as an operator, it is called to take the value corresponding to the address, so it can only be placed in front of the pointer variable, and the value pointed to by the address is taken out, which is the variable. The value of *point_year is the variable year. *point_year = 10; is actually assigning a value to the quantity year.

   A pointer variable is also a variable, so it also has an address. &point_year is to take the address corresponding to the pointer variable. It is an address, so we also need to declare a pointer variable int *ptr_ptr_year = &point_year. Then our * operator can also take the address *ptr_ptr_year returns point_year, but it is also a pointer variable, and *point_year is the pointed variable. **ptr_ptr_year is assigned to year, **ptr_ptr_year = 10, and it is also assigned to the variable year.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325312960&siteId=291194637