Introduction to Pointers
Pointers are a widely used type of data in the C language. Using pointer programming is one of the most important styles of C language. The use of pointer variables can represent a variety of data structures, can easily use arrays and strings, and can process memory addresses like assembly language, thus compiling a concise and efficient program. Pointers greatly enrich the functions of the C language. Learning pointer is the most important part of learning C language, whether the correct understanding and use of pointers is whether we master the C language of a symbol. At the same time, pointers are also the most difficult part of C language, in addition to the understanding of the basic concepts, but also must be more programming, on-Machine debugging. As long as this is done, the pointer is not difficult to grasp.
The basic concept 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 the number of memory units, such as the integral type of 2 units, characters accounted for 1 units, etc., in the second chapter has been detailed introduction. In order to access these memory cells correctly, you must have a number numbered for each memory unit. The internal deposit can be found accurately according to the number of a memory unit. The number of the memory unit is also called an address. Since the desired memory unit can be found based on the number or address of the memory unit, this address is often referred to as a pointer. The contents of the memory unit's pointer and memory unit are two different concepts. A popular example can be used to illustrate the relationship between them. When we go to the bank to deposit and withdraw money, the bank staff will find our deposit slip according to our account number and find out the amount of deposit and withdrawal on the deposit. Here, the account number is a certificate of deposit, the amount of deposits is the content of the deposit. For a memory unit, the address of the cell is the pointer, and the data stored is the content of the unit. In the C language, a variable is allowed to hold the pointer, which is called a pointer variable. Therefore, the value of a pointer variable is the address of a memory cell or a pointer called a memory cell. Figure, with the character variable C, the content is "K" (ASCII code is decimal number), C occupies 011A unit (address in 16 in number). With the pointer variable p, the content is 011A, which we call P point to Variable C, or P is a pointer to the variable C. Strictly speaking, a pointer is an address and is a constant. And a pointer variable can be given a different pointer value, which is variable. But the pointer variable is often referred to as a pointer. To avoid confusion, we agree that "pointers" are addresses, constants, and "pointer variables" that are variables that take a value as an address. The purpose of defining a pointer is to access the internal deposit by pointer.
Since the value of the pointer variable is an address, the address can be not only the address of the variable, but also the address of other data structures. In a pointer variable, store a
What is the meaning of the first address of a group or a function? Because arrays or functions are stored continuously. The array or function is also found by accessing the pointer variable to obtain the first address of the array or function. As a result, wherever an array appears, the function can be represented by a pointer variable, as long as the first address of the array or function is given in the pointer variable. Doing so will make the concept of the program very clear and the procedure itself concise and efficient. In C language, a data type or structure often occupies a contiguous set of memory units. The concept of "address" is not a good description of a data type or structure, and "pointer", although it is actually an address, but it is a data structure of the first address, it is "point to" a data structure, so that the concept of clearer, more explicit. This is also an important reason for introducing the concept of "pointers".
Description of type of pointer variable
The type description of the pointer variable includes three contents:
(1) The pointer type description, that is, the definition variable is a pointer variable;
(2) The name of the pointer variable;
(3) 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 descriptor represents the data type of the variable to which this pointer variable is directed.
For example, int *p1 means that P1 is a pointer variable whose value is the address of an integer variable. or P1 point to an integer variable. As to which integer variable P1, it should be decided by the address given to P1.
Another example:
Staic int *p2; /*P2 is a pointer variable that points to a static integer variable.
float *P3; /*P3 is a pointer variable that points to a floating-point variable *
Char *P4; /*P4 is a pointer variable that points to a character variable. It should be noted that a pointer variable can only point to a variable of the same type, such as P3 can only point to a floating-point variable, not sometimes to a floating-point variable, and then to a character variable.
Assignment of pointer variables
Pointer variables, like normal variables, are used not only to define a description, but also to assign a specific value. The unassigned pointer variable cannot be used, or it will cause system confusion or even panic. The assignment of a pointer variable can only be given to an address, and no other data can be assigned, or it will cause an error. In C, the address of a variable is assigned by the compilation system, completely transparent to the user, and the user does not know the specific address of the variable. The address operator & is provided in the C language to represent the address of the variable. The general form is: & variable name, such as &a variable A's address, &b represents the address of variable B. The variable itself must be described in advance. With a pointer variable p that points to an integer variable, there are two ways to assign the address of an integer variable A to P:
(1) method of initializing pointer variable int A;
int *p=&a;
(2) method of the assignment statement int A;
int *p;
p=&a;
It is not allowed to assign a number to a pointer variable, so the following assignment is wrong: int *p;p=1000; The assigned pointer variable cannot be added with a "*" specifier, as *p=&a is also an error
Operation of pointer variable
Pointer variables can perform certain operations, but the types of operations are limited. It can only carry out assignment operation and partial arithmetic operation and relational operation.
1. Pointer operators
(1) Take address operator &
The address operator & is the monocular operator, and its binding is from right to left, and its function is to take the address of the variable. We have already understood and used the & operator in the scanf function and the previous assignment of pointer variables.
(2) Fetch content operator *
The content operator * is the monocular operator whose binding is from right to left to represent the variable that the pointer variable refers to. The variable followed by the * operator must be a pointer variable. Note that the pointer operator * and the pointer specifier in the pointer variable description * are not the same thing. In the description of the pointer variable, "*" is a type specifier, indicating that the variable that follows is the pointer type. The "*" that appears in an expression is an operator that represents the variable that the pointer variable refers to.
Main () {
int a=5,*p=&a;
printf ("%d", *p);
}
......
Indicates that the pointer variable p gets the address of the integer variable A. This statement represents the value of the output variable A.