Pointer
Before learning pointers, you should first understand something:
What is memory?
From the hardware form, memory is a strip of physical equipment, functionally speaking, memory is a data warehouse, the program is loaded into memory before execution, to be executed by the CPU. Memory is made up of sequentially numbered sets of storage units in memory where each storage unit is represented by a unique address that can easily store information in a memory cell. In the computer, all information is in the form of binary data, each memory unit capacity is 1B, that is 8bit (8 0, 1 bits).
1, take address operator &, indirect operator *
& operator: Take the address operator, &m is the actual address of the variable m in memory.
* Operator: The pointer operator (often referred to as an indirect reference operator) that returns the value of the object to which its operand (that is, a pointer) points.
int num = 10;
printf ("%d,%x", num, &num);
* (&num) = 100;
printf ("%d,%x", num, &num);
&num is the address of the direct access variable num, while * (&num) is an indirect access
Direct access: Accesses variable values by variable address.
Indirect access: Variables are accessed by variables that hold variable addresses.
2. Declaration and initialization of pointer variables
When a pointer is declared, the compiler does not automatically complete its initialization, at which point the value of the pointer is indeterminate, that is, the pointer points to the memory unit that is completely random.
If you do not know at the beginning of a pointer variable declaration where to point this pointer, the simplest way is to place it "0", and the C language provides the keyword NULL
Its basic form is:
Type * pointer variable name;
int *pnum=null;
A pointer with a null value is called a null pointer, which means that the pointer does not point to any address. In header file stdio.h, NULL is defined as a constant.
3. Pointer variables must be initialized before they are used. Because the pointer is not initialized, it points to a random address, most likely the memory address that other programs are using, and if you modify it, it will cause the program to crash.
int num=100;
int * p;
p = num; This is wrong, can compile, run error, will be 100 as an address
p = # That's right
4, the pointer is just an address, the size is fixed, that is, four bytes.
int *p1;
Double *p2;
Char *p3;
sizeof (p1); Result is 4
sizeof (p2); Result is 4
sizeof (p3); Result is 4
5, the difference between the pointer and address
Two points: First, the pointer is a volume, corresponding to a memory area, two, the information stored by the pointer is the address of a memory unit.
For example: int num=10;
int *p=#
&num is an address that is a constant
and P is a pointer variable that can store an address
For example, 300500 is an address,
int *p= (int *) 300500 is a pointer, p stores the address, the pointer has a type, where to start, the length is how much, from where to end, after knowing the type, we know how the memory data is resolved
C Language Learning--pointers