Pointer and pointer variable

Source: Internet
Author: User
Generally, pointer variables are used to define pointer variables, assign values to pointer variables, and reference pointer variables. The description is as follows:
(1) define pointer Variables
In the variable definition statement int * P, * P1, * q;, all variables defined in * Are pointer variables. Therefore, this statement defines three integer pointer variables, namely P, P1, and Q. Because the pointer variable is used to store the variable address, and the address is usually 4 bytes, the length of the pointer variable is 4 bytes.
(2) pointer variable assignment
When a pointer variable is defined, its value is a random number. If the random number is the address of the system zone, the pointer variable is assigned a value to a storage unit in the system zone, the content in the unit in the system zone will be changed, which may cause system crash. Therefore, the address or 0 of a variable must be assigned after the pointer variable is defined.
From the example above, we can see that there are three situations for assigning an initial value to the pointer variable:
The first case is to assign the variable address to the pointer variable using the get address operator. For example, P = &;
The second case is to assign the address in a pointer variable to another pointer variable, for example, P1 = P;
In the third case, assign null values to the pointer variable, such as Q = 0, indicating that the pointer variable does not point to any variable.
After the assignment, point the pointer Variable P and P1 to variable A, and the Q value does not point to any unit, as shown in Figure 7.2.
(3) pointer variable reference
Pointer variables are referenced by the pointer operator. In the preceding example, both * P and * P1 represent variable A. Therefore, after the first output statement cout <* P is executed, the output value is 100 of the content of variable. The value assignment statement * P1 = 200 is to indirectly assign data 200 to variable A through the pointer variable P1. Therefore, in the second output statement, a, * P, * P1 are both values, and the content of variable A is 200.
(4) pointer variable Initialization
The pointer variable can assign an initial value when defining the pointer variable like a common variable. In the preceding example, the statement defining the pointer Variable P can be written as: int * P = &; 7.1.3 there are three types of pointer variable operations: Value assignment, relational and arithmetic operations.
1. pointer variable assignment
The pointer variable assignment operation assigns the address of the variable to the pointer variable. This article has been introduced in the previous section. Here is another example to deepen your understanding of the pointer variable assignment operation.
[Example 7.2] define three Integer Variables A1, A2, and A3, and use the pointer variable to complete the operations of a3 = A1 + A2. Define two real variables B1 and B2, and use the pointer variable to complete the operations of B1 + B2.
# Include <iostream. h>
Void main (void)
{Int a1 = 1, a2 = 2, A3;
Int * P1, * P2, * P3;
Float b1 = 12.5, b2 = 25.5;
Float * FP1, * fp2;
P1 = & A1;
P2 = & A2;
P3 = & A3;
* P3 = * P1 + * P2;
FP1 = & B1;
Fp2 = & B2;
Cout <"* P1 =" <* P1 <'\ t' <"* P2 =" <* P2 <' \ t' <"* P1 + * P2 = "<* P3 <'\ n ';
Cout <"a1 =" <A1 <'\ t' <"A2 =" <A2 <' \ t' <"A1 + A2 =" <<A3 <'\ n ';
Cout <"b1 =" <* FP1 <'\ t' <"b2 =" <* fp2 <' \ t' <"B1 + b2 = "<* FP1 + * fp2 <'\ n ';
}
After the program is executed, the output is:
* P1 = 1 * P2 = 2 * P1 + * P2 = 3
A1 = 1 A2 = 2 A1 + A2 = 3
B1 = 12.5 b2 = 25.5 B1 + b2 = 38
In this example, after pointer variable assignment, integer pointer variables P1, P2, and P3 point to the A1, A2, and A3 variables respectively. Therefore, * P1 = A1, * P2 = a2, * P3 = A3. Therefore, the * P3 = * P1 + * P2 operation is the A3 = A1 + A2 operation. 7.3 (. 2. arithmetic operations of pointer variables the arithmetic operations of pointer variables mainly include auto-addition, auto-subtraction, addition N, and subtraction N operations of pointer variables.
(1) Auto-increment operation of pointer Variables
Command Format: <pointer variable> ++;
The auto-increment operation of pointer variables does not add the value of pointer variables to 1, but points pointer variables to the next element. When the computer executes the <pointer variable> ++ command, the actual value of the pointer variable is the number of bytes of the pointer variable type, that is: <pointer variable> = <pointer variable> + sizeof (<pointer variable type> ). Assume that the first address of array a is 1000, 7.4. Int * P = & A [0]; // P = 1000, pointing to the [0] Element
P ++;
The first statement assigns the first address 1000 of array A to the pointer Variable P, so that p = 1000. The second statement makes p an auto-increment operation:
P = P + sizeof (INT) = p + 4 = 1004, so that P points to the next element a [1]. (2) Auto-subtraction of pointer Variables
Command Format: <pointer variable> ――;
The auto-subtraction operation of pointer variables is the operation that points pointer variables to the previous element. When the computer executes the <pointer variable> command, the pointer variable is actually reduced to the number of bytes of the pointer variable type, that is:
<Pointer variable >=< pointer variable>-sizeof (<pointer variable type>)
The auto-increment and auto-increment operations can be either post or pre-increment.
(3) add N operations to pointer Variables
Command Format: <pointer variable >=< pointer variable> + N;
The plus N operation of pointer variables is the operation of pointing pointer variables to the next n elements. When the computer executes the <pointer variable> + N command, the actual added value of the pointer variable is the number of bytes of the pointer variable type multiplied by N, that is:
<Pointer variable> = <pointer variable> + sizeof (<pointer variable type>) * n
(4) subtraction of pointer Variables
Command Format: <pointer variable >=< pointer variable>-N;
The minus n operation on Pointer variables refers to the operation on n elements. When the computer executes the <pointer variable>-N command, the actually reduced value of the pointer variable is the number of bytes of the pointer variable type multiplied by N, that is:
<Pointer variable> = <pointer variable>-sizeof (<pointer variable type>) * n
[Example 7.3] the auto-increment, auto-Subtract, N, and minus operations of pointer variables. Assume that the first address of array a is 1000, 7.4. # Include <iostream. h>
Void main (void)
{Int A [5] = {0, 1, 2, 3, 4 };
Int * P;
P = & A [0]; // P points to a [0], P = 1000
P ++; // P points to the next element a [1], P = 1004
Cout <* P <'\ T'; // output content 1 of a [1.
P = P + 3; // P points to the next three elements a [4], P = 1016
Cout <* P <'\ T'; // output content 4 of a [4.
P --; // P points to the previous element a [3], P = 1012
Cout <* P <'\ T'; // output content 3 of a [3.
P = P-3; // P indicates the top 3 elements a [0], P = 1000
Cout <* P <'\ T'; // outputs the content 0 of a [0.
}
Output after the program is executed:
1 4 3 0
As shown in the preceding example, the addition and subtraction of pointer variables can be used to move pointer variables to the next N element units or to the next N element units. 3. relational operation of pointer Variables
The relational operation of pointer variables compares the values of pointer variables, that is, the addresses in the two pointer variables. It is mainly used to judge the array elements. [Example 7.4] calculate the sum of one-dimensional real-type array elements using pointer variables, and output the values and arrays of each element in the array.
# Include <iostream. h>
Void main (void)
{Int A [5] = {1, 2, 3, 4, 5 };
Int * P, * P1;
P1 = & A [4] + 1;
For (P = & A [0]; P <P1; P ++)
Cout <* P <'\ T ';
Int sum = 0;
P = & A [0];
While (P! = P1) sum + = * P ++;
Cout <"\ n sum =" <sum <Endl;
}
After the program is executed: output:
1 2 3 4 5
Sum = 15
In the program, first assign the array tail address + 1 to P1, as shown in 7.4. In the for statement, the pointer Variable P is a loop variable, and the first address of the array & A [0] is assigned to P. When looping, compare the loop control variable P with the address in P1. If P <P1, the value of array element a [I] is output in * P mode, and point P auto-increment to the next element. This cycle ends until p ≥ P1.
In the while statement, P is still used as the loop control variable. When p! = P1, use sum = sum + * P; statement to accumulate the array element value to sum, and use P ++ statement to point the pointer Variable P to the next element, loop until P = p1, and finally the sum of the output array elements. 4. Hybrid Operation and priority of pointer operators (1) the pointer operator * has the same priority as the get address operator and is combined in the right-to-left direction.
Statement with variable definition: int A, * P = &;
Then the expression: the order in which & * P is evaluated is "*" followed "&", that is, & (* P) = & A = P.
And the expression: * & A is evaluated in the order of first "&" and then "*", that is, * (& A) = * P =.
(2) "+ +", "-", "*", and "&" have the same priority and are combined from the right to the left. The following is an example. Variable Definition Statement:
Int A [4] = {100,200,300,400}, B;
Int * P = & A [0];
For ease of description, assume that the first address allocated to array a is 1000, 7.4.
① B = * P ++;
Based on the combination of right and left, the expression * P ++ calculates the Value Order first "+ +" and then "*", that is, * (p ++ ). Because "++" is followed by P as the post ++ operator, the actual operation of the expression is to take the * P value first, and then perform the P ++ auto-adding operation. That is, the value assignment expression B = * P ++; is equivalent to the following two statements:
B = * P; // B = * P = A [0] = 100
P ++; // P = P + sizeof (INT) = 1004
The final calculation result is B = 100, and P = 1004 points to a [1].
② B = * ++ P;
Based on the combination of right and left, the expression * ++ P is evaluated in the order of "+ +" and "*", that is, * (++ P ). Because ++ is a front-end ++ operator before P, the actual operation of the expression is the addition operation of ++ P, and then the * P value is obtained. That is, the value assignment expression B = * ++ P; is equivalent to the following two statements:
+ + P; // P = P + sizeof (INT) = 1008, pointing to a [2]
B = * P; // B = * P = A [2] = 300
The final calculation result is B = 300, and P = 1008 points to a [2]. ③ B = (* P) ++;
Given the priority operation in parentheses, the expression first extracts the value of * P (that is, a [2]) and assigns it to B, then add the value of * P, that is, the content of a [2], to 1. Therefore, the expression is equivalent to the following two statements:
B = * P; // B = A [2] = 300
A [2] ++; // A [2] = 300 + 1 = 301
④ B = * (p ++ );
① We can see that this expression is equivalent to * P ++ and the calculation result is:
B = * P; // B = A [2] = 301
P ++; // P = P + sizeof (INT) = 1012, pointing to a [3]
⑤ B = ++ * P;
This expression first performs the "*" operation, and then performs the "++" operation, that is, the * P value is obtained first, and then the value is added to 1. Therefore, the expression actually performs the following operation: B = ++ (* P) = ++ A [3] = 400 + 1 = 401; P still points to a [3.
The preceding statements are summarized as follows: [Example 7.5] pointer operator "*", "&", "++" priority and combination Law examples.
# Include <iostream. h>
Main ()
{Int A [4] = {100,200,300,400}, B;
Int * P = & A [0];
Cout <'\ t' <"P =" <p <Endl;
B = * P ++;
Cout <"B =" <B <'\ t' <"P =" <p <Endl;
B = * ++ P;
Cout <"B =" <B <'\ t' <"P =" <p <Endl;
B = (* P) ++;
Cout <"B =" <B <'\ t' <"P =" <p <Endl;
B = * (p ++ );
Cout <"B =" <B <'\ t' <"P =" <p <Endl;
B = ++ * P;
Cout <"B =" <B <'\ t' <"P =" <p <Endl;
}
The running result is:
P = 0x0065fde8
B = 100 P = 0x0065fdec
B = 300 P = 0x0065fdf0
B = 300 P = 0x0065fdf0
B = 301 P = 0x0065fdf4
B = 401 p = 0x0065fdf4
Note: When defining an array, the address of Data A is dynamically allocated by the operating system storage management. Therefore, the address of array a is uncertain and the results of each operation may be different. It is usually expressed in hexadecimal notation. In this example, the first address allocated to array a is 0x0065fde8. The address assumed in Figure 7.4 is 1000 ~ 1016 is intended to be easy for readers to understand.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.