[Hard core] What is the C language pointer? Explain in simple terms and take you to master C language pointers!

Pointers are closely related to the underlying hardware, and pointers can be used to manipulate data addresses to achieve indirect access to data. The content of this article is as follows


1. The role of pointers in C language
2. The storage mechanism of computers
3. How to define pointers
4. How to operate pointers
5. The relationship between arrays and pointers
6. Some precautions in the use of pointers


1. What is the function of C language pointer?

(1) Passing parameters
1> Use pointers to pass large-capacity parameters. The main function and sub-functions use the same set of data, which avoids data copying during parameter passing, improves operating efficiency, and reduces memory usage 2> Use pointers to
pass Output parameters, using the same set of data characteristics of the main function and sub-functions, realize the return of data, and realize the design of multi-return value functions (2) Pass the
return value
to return the public part in the module, so that the main function holds the module's " (
3) Directly access the data under the physical address
1> access the data under the hardware specified memory, such as the device ID number, etc.
2> convert the data in a complex format into bytes, which is convenient for communication and communication storage

2. Computer storage mechanism

insert image description here
Computer memory is in units of bytes (a byte is equal to 8 bits, divided into high four bits and bottom four bits), and each byte corresponds to an address For non-array variables, the little end
is placed first Bits (little-endian) are put into addresses front-to-back
for array variables , and for two-byte variables are stored the same way as non-array variables (little-endian first)

3. Define the pointer

A pointer is a pointer variable, which is used to store the first address of other data units (variables/arrays/structures/functions, etc.) . If the pointer stores the first address of a data unit, the pointer points to the data unit. If the value stored in the pointer is 0, the pointer is a null pointer
insert image description here

4. Pointer operation

insert image description here
Pointer data address storage method:

char a=0x66char *p=&a;

insert image description here
When printing *p, the content stored in the p address is taken out as the address to find the content

5. Arrays and pointers

An array is a collection of variables of the same data type , and its array name is a pointer to the data type. The definition of an array is equivalent to applying for memory, defining pointers, and initializing.
insert image description here
The relationship between arrays and pointers is shown in the figure below
. 1. Arrays can be represented by pointers:
insert image description here
2. Pointers can be represented by arrays:
insert image description here

6. Precautions when using pointers

1. When using the pointer, make sure that the address pointed to by the pointer has data.
2. When defining the pointer, you must assign a value to the pointer, otherwise it will cause the pointer to point to an indeterminate address, resulting in a program error
. Assignment may cause errors

int a;
int *p;
a=*p;//把指针p指向地址取出,去找地址指向寄存器中的内容并取出,赋值给a
p=&a;//把数据a的首地址赋值给p

insert image description here

Guess you like

Origin blog.csdn.net/qq_45156021/article/details/130508616