Basic concepts of pointers
In the computer, all the data is stored in the memory. In general, the memory of a byte is called a memory unit, different data types occupy a number of memory units, such as the integer amount of 2 units, the character of 1 units, etc., in the previous detailed introduction. In order to access these memory cells correctly, each memory unit must be numbered. The internal memory element can be found accurately based on the number of a single cell. The number of the memory unit is also called the address. Since the memory unit can be found according to the number or address of the memory unit, it is often referred to as a pointer.
Pointers and variables
In the C language, a variable is allowed to hold a pointer, which is called a pointer variable. Therefore, the value of a pointer variable is the address of a memory cell or a pointer to a memory unit.
Note:
A pointer is an address and is a constant. A pointer variable can be given a different pointer value, which is a variable. But the pointer variable is often referred to as a pointer. In order to avoid confusion, we agree that "pointer" means an address, a constant, and a "pointer variable" refers to a variable that takes a value as an address. The purpose of defining pointers is to access the internal deposit cells via pointers.
Defining a pointer variable defines a pointer variable to include three contents:
- Pointer type description, which defines the variable as a pointer variable;
- pointer variable name;
- The data type of the variable that the variable value (pointer) points to.
The general form is:
Type descriptor * variable name;
where * indicates that this is a pointer variable, the variable name is the defined pointer variable name, and the type specifier represents the data type of the variable to which this pointer variable is pointing.
int i =3,j=6,k; &i = 2000
K = i+j;
int *i_pointer = &i; Whose address is the equivalent of pointing to WHO
I_pointer--->i
Note "*" Two use occasions
1) When defining a pointer variable, use the INT * variable name to indicate that the variable is a pointer variable.
2) * Pointer variable name accesses the storage space that the pointer variable points to
Considerations for pointer variables:
1) What type of pointer variable can only point to a variable of the same type
2) The pointer variable can be global or local
Two related operators:
- &: Take address operator;
- *: pointer operator (or "indirect access" operator).
The address operator & is provided in the C language to represent the address of the variable. Its general form is: & variable name; If &a represents the address of variable A, &b represents the address of variable B.
Initialization method for pointer variables:
1) initialization at the same time as defined
int a=10;
int *p = &a;
2) First define the post-initialization
int a=10;
int *p;
int *q;
p=&a;
q=&a;
3) If you are unsure what the pointer is pointing to
p = NULL;
Dark Horse Programmer C Language: The concept of pointers