Completely understand the C language pointer in 1 minute

All data in the computer must be placed in memory. The number of bytes occupied by different types of data is different. For example, int takes 4 bytes and char takes 1 byte. In order to access these data correctly, each byte must be numbered, just like the house number and ID number, each byte number is unique, and a certain byte can be accurately found based on the number.

FIG 4G is the memory for each byte number (in hexadecimal):
The number of each byte in 4G memory

We called the number of bytes of memory address (Address) or pointer (Pointer). The address increases in order from 0. For a 32-bit environment, the program can use 4GB of memory, the smallest address is 0, and the largest address is 0XFFFFFFFF.

The following code demonstrates how to output an address:

  1. #include <stdio.h>
  2. int main () {
  3. int a = 100;
  4. char str[20] = "c.biancheng.net";
  5. printf("%#X, %#X\n", &a, str);
  6. return 0;
  7. }

Operation result:
0X28FF3C, 0X28FF10

%#Xmeans output in hexadecimal form, with prefix 0X. a is a variable, used to store integers, you need to add in front &to get its address; str itself represents the first address of the string, you do not need to add &.

C language has a control character %p , dedicated to sourcing address in hexadecimal form, but the% p output format is not uniform, with some compiler 0x prefix, some without, so here we did not use.

Everything is an address

The C language uses variables to store data, and functions to define a piece of code that can be reused. They must eventually be placed in memory to be used by the CPU.

Both data and code are stored in memory in binary form, and the computer cannot distinguish in the format whether a piece of memory stores data or code. When the program is loaded into memory, the operating system will assign different permissions to different memory blocks. The memory blocks with read and execute permissions are the code, while the read and write permissions (may only have read permissions) The memory block is the data.

The CPU can only obtain the code and data in the memory through the address. During the execution of the program, the CPU will be informed of the code to be executed and the address of the data to be read and written. If the program accidentally makes an error, or the developer intentionally does so, when the CPU wants to write data, give it an address in the code area, and a memory access error will occur. This kind of memory access error will be intercepted by the hardware and the operating system, forcing the program to crash, and the programmer has no chance of salvation.

When the CPU accesses memory, it needs addresses, not variable names and function names! Variable names and function names are just a kind of mnemonic address. When the source file is compiled and linked into an executable program, they will be replaced with addresses. An important task of the compilation and linking process is to find the addresses corresponding to these names.

Assuming that the addresses of variables a, b, and c in memory are 0X1000, 0X2000, and 0X3000, respectively, the addition operation c = a + b;will be converted into a form similar to the following:

0X3000 = (0X1000) + (0X2000);

( )Represents the value operation. The whole expression means to take the values ​​at addresses 0X1000 and 0X2000, add them, and assign the result of the addition to the memory

variable name and function name at address 0X3000. We provide convenience, let us In the process of writing code, you can use English strings that are easy to read and understand. You do n’t have to face the binary address directly.

It should be noted that although variable names, function names, string names, and array names are essentially the same, they are all mnemonics of addresses, but in the process of writing code, we think that variable names represent the data itself , And the function name, string name and array name represent the first address of the code block or data block.

Guess you like

Origin www.cnblogs.com/sea520/p/12678536.html