Pointer and pointer variable (rpm)

Commonly used indicator variable is: to define a pointer variable, the pointer to the variable assignment, the last reference pointer variables. Will now be described as follows:
  (1) the definition of indicator variables
  defined variable statement int * p, * p1, * q; variables are defined by pointer variable *. Thus the statement defines three integer pointer variable named p, p1 with q. Since the pointer variable used to store the variable address, and the address is generally 4 bytes, the pointer variable length of 4 bytes.
  (2) a pointer variable assignment
  after the pointer variable is defined which is a random number, if this random number is the address area of the system, the system of the pointer variable referred Area memory cell assignment operator, the system will change the contents of the cell area It could lead to the collapse of the system. So, after a pointer variable definition must be assigned an address 0 or a variable.
  As can be seen from the above example, the initial values to the pointer variable in three cases:
  the first case is address operator "&" is assigned to the variable address pointer variable. As: p = & a;
  the second case the variable is a pointer to the address assigned to another pointer variable, such as: p1 = p;
  third case is assigned to a pointer variable null value 0 as q = 0; represents the pointer does not point to any variable variable.
  After the assignment, the pointer variable p, p1 points to the variable a, q does not point to any cell, shown in Figure 7.2.
  Reference (3) a pointer variable
  pointer variable is referenced by a pointer operator "*" to achieve. In the above example, * p * p1 are represented with a variable, therefore, the first output cout << * p statement is executed, the output 100 is the contents of a variable. The assignment statement * p1 = 200; p1 through indirect pointer variable data 200 is assigned to a variable, therefore, the second output statement, a, * p, * p1 with the contents of a variable assignment of 200.
  (4) initialize the pointer variable
  pointer variable may be the same as ordinary variable, when defining the initial value pointer variable, the example above, the definition statement pointer variable p can be written as: int * p = & a;
7.1.3 pointer variable calculation

  Operation pointer variable in three ways: assignment operators, relational operators and arithmetic.
  1. Pointer variable assignment operator
  pointer variable assignment operator is the address of the variable to the pointer variable, the section has been introduced, it is another example to deepen the reader's understanding of the assignment operator pointer variables.
  [Example 7.2] defines three integer variables a1, a2, a3, a3 = a1 + a2 operation completion pointer variable. Redefinition two real variable b1, b2, pointer variable operation b1 + b2 is completed.
 The include # <, iostream.h>
 void main (void)
 {int = A1. 1, A2 = 2, A3;
  int P1 *, P2 *, P3 *;
  a float = 12.5 B1, B2 = 25.5;
  a 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 <
  cout<<"b1=" <<*fp1<<'\t'<<" b2="<<*fp2<<'\t'<<"b1+b2="<<*fp1+*fp2<<'\n';
 }
 
  After the execution, output:
  * P1 =. 1 * P2 = 2 * P1 + * P2 =. 3
  A1 =. 1 A2 = 2 A1 + A2 =. 3
  B1 = 12.5 B2 = 25.5 B1 + B2 = 38 is
  in this embodiment, after the pointer variable after the assignment operation, an integer pointer variables p1, p2, p3 pointing variables a1, a2, a3, therefore, * p1 = a1, * p2 = a2, * p3 = a3. Therefore, * p3 = * p1 + * p2 operation is operation a3 = a1 + a2. FIG. 7.3 (a) shown in FIG.
    2. Pointer variable arithmetic

  Arithmetic mainly pointer variable pointer variable is incremented and decremented. N plus and minus n operations.
  (1) from the pointer variable add operation
  instruction format: <pointer variable> ++;
  pointer variable from the add operation does not add value calculating a pointer variable, but the pointer variable to point to the next element of the calculation. When the computer execute <pointer variable> ++ instruction pointer variable is the actual increase in the number of bytes pointer variable types, namely: <pointer variable> = <pointer variable> + sizeof (<pointer variable type>). Suppose a first address of the array 1000, shown in Figure 7.4.
int * p = & a [0 ]; // p = 1000, pointing to a [0] element
  p ++;
  The first statement of a first address array 1000 to the pointer variable p, so that p = 1000. The second statement made from the p-add operation:
  p = p + the sizeof (int) = p + = 1004. 4, a point p so that the next element a [1].
   (2) a pointer variable decrement
  instruction format: <pointer variable> -;
  decrement pointer variable is a pointer variable to point to the computing element. When a computer to execute <pointer variable> - the instruction pointer variable is a pointer variable to reduce the actual number of bytes types, namely:
  <pointer variable> = <pointer variable> -sizeof (<pointer variable type>)
  from the add operation and decrementing post operation can also be front.
  (3) a pointer variable is incremented by n arithmetic
  instruction format: <pointer variable> = <pointer variable> + n;
  add operation pointer variable n is the pointer variable to point to the next calculation of n elements. When the computer execute <pointer variable> + n instruction pointer variable is the actual increase in the number of bytes the pointer variable type is multiplied by n, namely:
<pointer variable> = <pointer variable> + sizeof (<pointer variable type>) * n
  (4) a pointer variable n Save operation
  instruction format: <pointer variable> = <pointer variable> -n;
  Save operation n a pointer variable is a pointer variable to point to the calculation of n elements. When a computer to execute <pointer variable> - n after instruction pointer variable number of bytes of actual reduction type pointer variable is multiplied by n, namely:
  <pointer variable> = <pointer variable> -sizeof (<pointer variable type>) * n
[Example 7.3] was added from a pointer variable, decrement, addition, and subtraction n n operations. Suppose a first address of the array 1000, shown in Figure 7.4.

 The include # <, iostream.h>
 void main (void)
 {int A [. 5] = {0,1,2,3,4};
  int * P;
  P = & a [0]; // to point P a [0] , P = 1000
  P ++; // p points to an element at a [1], P = 1004
  COUT * P << << '\ T'; // output a [1] 1 content.
  p = p + 3; // p points to the three elements A [4], P = 1016
  COUT * P << << '\ T'; // Output A [4] 4 content.
  the P--; // p points to an element on a [3], P = 1012
  COUT * P << << '\ T'; // content output a [3] 3.
  p = p-3; // p points to the three elements a [0], P = 1000
  COUT * P << << '\ T'; // outputs a [0] 0 content.
 }
  After program execution Output:
  1,430
  from the above example can be seen that by addition and subtraction arithmetic operations on pointer variable, the variable can be achieved move the pointer points to the next object of the n elements or units of n elements up unit.
3. Relational operators pointer variable
  relational operators pointer variable is a pointer variable size comparison value, i.e., address pointers in the two comparison variables, for judging the main array elements.

  [Example 7.4] one - dimensional real array elements using pointers and variables, and the output value of each element of the array, and the array, and.
 The include # <, iostream.h>
 void main (void)
 {int A [. 5] = {1,2,3,4,5};
  int * P, * P1;
  P1 & A = [. 4] + 1'd;
  for (P P <P1;; = & A [0] P ++)
  COUT << * P << '\ T';
  int SUM = 0;
  P = & A [0];
  ! the while (P = P1) SUM + = * P ++;
  COUT < < "\ n-SUM =" SUM << << endl;
 }
  after executing the program: output:
  . 1 2. 5. 4. 3
  SUM = 15
  program first end of the array is assigned an address +1 p1, shown in Figure 7.4. In the for statement, the pointer variable p is the loop variable, the first address of the array & a [0] is assigned to p. First circulation loop control variable p and p1 comparing the address, if p <p1, * p manner places the output array element a [i] value, and adding from p point to the next element. This loop until p≥p1.
  In the while statement, it is still used as a loop control variable p, while when p = p1, a sum = sum + * p!;
4. Mixed computing pointer operator with a priority

  (1) The operator * Pointer to priority address operator & same, combined in a direction from right to left.
  With variable definition statement: int a, * p = & a;
  expression: * p & evaluation order is first "*" after the "&", i.e. & (* p) = & a = p.
  The expression: & A * after evaluation order for the first "&", "*", i.e., * (& a) = * p = a.
  (2) "++", - the same, "*", "&" priority combined in a direction from right to left "." It will be described below with examples. With variable definition statement:
  int a [. 4] = {100,200,300,400}, B;
  int * P = & a [0];
  For convenience, assume that the system assigned to a first address of the array 1000, shown in Figure 7.4.
  ① b = * p ++;
  press bonded principle from right to left, the expression evaluates * P ++ cis-first order "+", "*", that is: * (p ++). Since "+" is the post after p ++ operator, the actual operation is to take the expression * p value, added after the operation since the p ++. That assignment expression b = * p ++; equivalent to the following two statements:
  B = P *; P = // * B = A [0] = 100
  P ++; // P + P = the sizeof (int) = 1004
  Last operation the result is b = 100, p = 1004 points to a [1].
  ② b = * ++ p;
  From right to left according to the principle of combination of the expression ++ * P evaluation order after the first "+", "*", that is: * (++ p). Since prior to the p ++ ++ operator for the front, so the actual operation of self-expression is added to the operation of p ++, * p value after taking. That assignment expression b = * ++ p; equivalent to the following two statements:
  ++ P; P = P + // the sizeof (int) = 1008, pointing to A [2]
  B = P *; * // B = p = a [2] = 300
  last operation is b = 300, p = 1008 points to a [2].
③ b = (* p) ++ ;
  since the priority operation in parentheses, the expression * p to remove (i.e., a [2]) and assigns the value of B, then the value of * p, i.e., a [2] Content plus 1. Therefore, the following two statements equivalent to the expression:
  B = P *; // B = A [2] = 300
  A [2] ++; // A [2] = + 300 = 301. 1
  ④ * B = (P ++ );
  understood from ①, which is equivalent to the expression * p ++, the operation results in:
  B = P *; // B = a [2] = 301
  P ++; // P + P = the sizeof (int) = 1012, pointing to a [. 3]
  ⑤ B ++ = * p;
  the first expression a "*" operator, then the "+" operator, i.e., to remove the value of * p, then the value is incremented. Thus the following expression is actually performed operation: b = ++ (* p) = ++ a [3] = 400 + 1 = 401; p is pointing to a [3] unchanged.
  The above discussion of each sentence examples are summarized as follows:

 
  【例7.5】指针运算符"*"、"&"、"++"优先级与结合律示例。
 # 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 = 300 0x0065FDF0   B 0x0065FDF0 = 300 = P   B = 100 P = 0x0065FDEC   P = 0x0065FDE8   Run Results It is:   }   COUT << "B =" << << B '\ T' << "= P" P << << endl;
  B = P * ++;







  0x0065FDF4 = 301 = P B
  B P = 0x0065FDF4 = 401
  Description: the array definition, a data address is dynamically assigned by the operating system storage management, and therefore, the address of array a is uncertain, the result of each operation are It may be different. Generally expressed in hexadecimal numbers. In the present embodiment the first address of the array system assigned to a 0x0065FDE8. In Figure 7.4 is assumed as 1000 to 1016 address solely for convenience of the reader.

Reproduced in: https: //www.cnblogs.com/kungfupanda/archive/2011/11/29/2267825.html

Guess you like

Origin blog.csdn.net/weixin_34161029/article/details/94494179