Yesterday, I just consulted Mr. Liu and had a deeper understanding of the insecure operations of C language pointers.
Pointer beginners often take char * P = "Hello World" as a matter of course, and are overwhelmed by the "this memory cannot be written" error.
In fact, it is a pointer pointing to the starting address of the String constant. The content of this address is read-only and cannot be written.
Pointer:
First, we need to know that char * P declares a pointer, but we do not require it to point to which memory area, so it randomly points to a certain place. This part may be either a blank memory area or a region where data is already stored. If it is the former, it does not matter. If it is the latter, it will overwrite the data so thatProgramDamaged, causing a crash. This is the so-called insecure pointer application.
So how can we use pointers safely?
In fact, it is very easy to remember, "always let the pointer point to the place you want", instead of letting it randomly point to a place. This prevents the destruction of useful data and eliminates unsafe factors. For example:
Char string [] = "Hello World ";
Char * P;
P = string;
Okay, so that * P points to the header of the string [] we opened, everything is safe (unless you increase the increment when adding the pointer address to the end of the memory occupied by the string or to the front of the memory ).
If you do not learn pointers well, you will not be able to learn the C language well. In advanced languages, only the C language can be so flexible and powerful to directly control the hardware, which is the first language for MCU control. This is the future of C learning.
Be good at C, be a driver, and be an OS