C ++ -- pointer Review
I. Basic Concepts
1. Memory storage Principle
If you define a variable in the program, allocate the memory unit to the variable during compilation. The system allocates a certain length of space based on the variable type defined in the program. For example, the C ++ compilation system generally allocates four bytes for the integer variable, four bytes for the single precision Floating Point variable, and one byte for the variable. Each byte in the memory area has a number, which is the "Address", which is equivalent to the room number in the hotel. Store data in the memory indicated by the address, which is equivalent to the number of passengers in each room in the hotel.
Difference: the address of the memory unit and the content of the memory unit
Assume that the program has defined the 3ge integer variable, I, j, k. during compilation, the system allocates four bytes, namely, and, to the variable I, and allocates these four bytes to j, distribution ., the size of the message is kb. In a program, the variable name is generally used to access the memory unit. After compilation, the program has converted the variable name to the variable address, and the access to the variable value is carried out through the address. For example, the execution of the statement "cout <I"; is like this: According to the correspondence between the variable name and the address (this correspondence is determined during compilation ), find the address of variable I 2000, then retrieve the data from the four bytes starting from 2000 (that is, the value of the variable 3), and output it. If "cin> I;" is used for input, the input value from the keyboard is sent to the integer storage address starting from 2000. If there is a statement: "k = I + j;", the I value will be retrieved from the storage of Integer Variables starting from 2000 bytes (3 ), take out the j value (6) from the 2004-Byte variable storage, add them, and then sum them (9) it is sent to the integer storage unit starting with 2008 bytes occupied by k. This method of accessing variable values by variable address is called direct access or direct access.
You can also use the introduction access (indirect access) method to store the address of variable I in another variable. You can define such a special variable in a program, which is used to store addresses. Assume that an I _pointer variable is defined to store the address of an integer variable. The compilation system allocates four bytes to the variable (assumed to be 3010-3013 bytes ). You can use the following statement to store the start address (2000) of I in I _pointer.
I _pointer = & I;
& Is the address operator, & I is the address of variable I. After this statement is executed, the I _pointer value is 2000 (that is, the starting address occupied by variable I ). To obtain the value of variable I, in addition to the direct method, you can also use the indirect method: first find the variable I _pointer that stores the address of "I, extract the address (2000) of I from it, and then obtain the value (3) of I from the four bytes starting with 2000 ).
Pointer definition: the address of a variable is called the pointer of the variable.
Pointer variable: a variable that is used to store another variable address (that is, a pointer) is called a pointer variable.
Difference: pointer and pointer variable
The value of the pointer variable (that is, the value stored in the pointer variable) is the address (that is, the pointer ). For example, the pointer to variable I is 2000, but the pointer variable of variable I is 2000.
Variables and pointers
The pointer to a variable is the address of the variable. The variable used to store the variable address is a pointer variable. A pointer variable is a special variable. It is different from other types of variables that have been learned before: it is used to point to another variable. To indicate the relationship between the pointer variable and the variable it points to, use the "*" symbol in C ++ to indicate the link. For example, I _pointer is a pointer variable, * I _pointer indicates the variable pointed to by I _pointer. * I _pointer also represents a variable, which is the variable I pointed to by I _pointer.
The following two statements share the same role:
I = 3;
* I _pointer = 3;
The second statement is to assign 3 to the variable I pointed to by the pointer variable I _pointer.
Define a pointer variable:
C ++ requires that all variables must be defined before use, that is, their types are specified. The storage space is allocated according to the variable type during compilation. Pointer variables must be defined as pointer types.
Int I, j; // defines the integer variable I, j
Int * pointer_1, * pointer_2; // defines the pointer variable * pointer_1, * pointer_2
The int at the beginning of the second row refers to the pointer variable defined to point to the integer data, or * pointer_1, * In pointer_2, only the addresses of integer data (such as integer variables or integer array elements) can be stored, but not the addresses of floating point or other types of data. This int Is the base type of the pointer variable. The base type of the pointer variable is used to specify the variable type that the pointer variable can point.
The general form of defining pointer variables:
Base type * pointer variable name;
Point A pointer variable to another variable:
Pointer_1 = & I; // store the address of variable I in pointer_1
Pointer_2 = & j; // store the address of variable j in Pointer_2.
In this way, pointer_1 points to variable I, and pointer_2 points to variable j.
Generally, the C ++ compilation system allocates four bytes for each pointer variable to store the variable address.
Note the following when defining pointer variables:
1. An integer cannot be used to assign an initial value to a pointer variable. For example, int * pointer_1 = 2000; is incorrect. If you write this statement, you may want to use address 2000 as the initial value of the pointer variable pointer_1, however, the compilation system does not regard 2000 as an address (byte number) but an integer. Therefore, it is regarded as a syntax error and an error message is displayed, you can use the address of a defined variable as the initial value of the pointer variable.
Int I; // defines Integer Variables
Int * pointer_1 = & I; // use the address of variable I as the initial value of the pointer variable pointer_1
2. You must specify the base type when defining pointer variables. Cause: different types of data are stored in different computer systems and occupy different bytes.
The definition of the pointer variable "int pointer_1, pointer_2;" can also be understood as follows:
Pointer_1 and pointer_2 are integer variables, just like int a and B. They define a and B as integer variables. * Pointer_1 and * pointer_2 are the variables pointed to by pointer_1 and pointer_2. Obviously, pointer_1 and pointer_2 are pointer variables. Note: Only the address of the integer variable can be placed in the pointer variable whose base class is integer.
For convenience, pointer_1 and pointer_2 are pointer variables. In fact, pinter_1 and pointer_2 are pointer variables pointing to integer data.
Reference pointer type
Operator number: & obtain the address Operator
* Pointer operators (or access operators)
For example, & a is the address of variable a, and * p is the storage unit pointed to by the pointer Variable p.
Example: Use Pointer variables to access integer variables.
/*
* Pointer_1.cpp
*
* Created on: 2012-4-4
* Author: David
*/
# Include <iostream>
Usingnamespace std;
Int main ()
{
Int a, B; // defines Integer Variables a, B
Int * pinter_1, * pointer_2; // defines the pointer variable * pinter_1, * pointer_2
A = 100; B = 10; // assign a value to a and B to www.2cto.com
Pointer_1 = & a; // assign the address of variable a to pointer_1
Pointer_2 = & B; // assign the address of variable B to pointer_2
Cout <a <"" <B <endl; // outputs values of a and B
Cout <* pinter_1 <"" <* pointer_2 <endl; // outputs the values of * pointer_1 and * pointer_2
Return 0;
}
Program description:
1. Although two pointer variables pointer_1 and pointer_2 are defined in the program, they do not point to any integer variable, but only provide two pointer variables whose base type is integer. They can point to integer variables. You must specify the integer variables in the Program Statement.
2. pointer_1 and pointer_2 are variables a and B. The last two cout statements have the same effect.
3. pointer_1 = & a and pointer_2 = & B assign the addresses of a and B to pointer_1 and pointer_2 respectively. Note that it should not be written as: * pointer_1 = & a; and * pointer_2 = & B; because the address of a is assigned to the pointer variable pointer_1 rather than * pointer_1 (variable ).
Descriptions of operators "*" and:
If the "pointer_1 = & a;" statement has been executed, what does & * pointer_1 mean ?" The priority levels of the operators "&" and "*" are the same, but they are combined from the right to the left. Therefore, the * pointer_1 operation is performed first, that is, variable a is executed again. Therefore, & * pointer_1 is the same as & a, that is, the address of variable.
If pointer_2 = & * pointer_1;
It assigns & a (address of a) to pointer_2. If pointer_2 originally points to B, it no longer points to B after being assigned a value, but it also does not point to.
What does & a mean? Perform the & a operation first, obtain the address of a, and then perform the * operation, that is, the variable pointed to by & a. * & a and * pointer_1 play the same role, they are equivalent to variable. That is, * & a is equivalent to.
Instance: sorting of the size
Input two integers, a and B, and output the values a and B in the ascending order (using pointer variables)
Solution: set two pointer variables p1 and p2 to point to a and B respectively, so that p1 points to the big ones in a and B, p2 points to the small ones, and output * p1 in sequence, * p2 Outputs a and B in ascending order. Compile the program as follows:
/*
* Pointer_2.cpp
*
* Created on: 2012-4-4
* Author: David
*/
# Include <iostream>
Usingnamespace std;
Int main ()
{
Int * p1, * p2, * p, a, B;
Cin> a> B;
P1 = &;
P2 = & B;
If (a <B)
{
P = p1;
P1 = p2;
P2 = p;
}
Cout <"a =" <a <"B =" <B <endl;
Cout <"max =" <* p1 <"min =" <* p2 <endl;
Return 0;
}
From Anno's column