First of all, what is a pointer?
What the programming teacher may tell you is the storage location of this variable. So is this statement wrong? No, then how to understand it?
A computer program will be put into memory by the operating system when it is running, regardless of whether the program uses program code, variables or constants. They are all in the stack in memory. So what is memory? Memory is a series of sequentially numbered storage units. For example, if I want to store an integer in memory, it will take up four memory cells (each cell size is one byte, which is 8 bits), so we say that the length of int is 4. Similarly, other types of data also have corresponding lengths, such as the length of double data is 8.
Then it is impossible to put only one piece of data in memory. After putting in multiple data, how to find the desired variable position?
The simple way is that I write down the starting position of the variable. For example, if I record a double-precision floating-point number data, I only need to record its starting position (assuming it is 12), then I can read the 8-byte storage unit from 12, and I can read the variable value I want. So, what is this 12? This 12 is the variable position, so we gave it an elegant name: pointer.
Next, the pointer itself is just an address, which is just a number. Since it is a number, it must also be stored in memory-computers cannot be stored in registers. So pointers are also variables, and by default, they are not essentially different from other variables. The computer only knows the number stored here, and doesn't care whether it represents an address or a storage location needed by human beings.
So since it is a storage location, why define different pointer types? For example, if I tell you that there is a variable in the memory location of 12, how can I read the value of this variable? Should I read in 4 bytes or 8 bytes? Therefore, it is necessary to specify what the variable type is (int or double), so the value of this pointer is 12, but whether the type is int* or double* still needs to be specified manually.
But no matter what kind of pointer (even a pointer of structure type), their length is the same (the length of int * is the same as that of double *). So we can forcibly read double* type as int* type. For example, I stored a number of type double in the memory space 12- 19, but I read only 4 bytes from 12, which may be interpreted as int. This is the meaning of this sentence in the title.
So why is the result 0? We only read the storage contents of 12- 15, regardless of the original number of length 8 (after all, the storage methods of int and double are different). Therefore, we must first understand the storage mode of double in 12- 19, find out the contents stored in 12- 15, and then put these four numbers together "out of context" as a plastic number. (In this case, the result is 0. )
Do you understand?