--C ++ review arrays and pointers (3)

First, the array

 

       1. The length of the array is fixed, but can be like the pointer iterators to traverse the array;

       2. The array can be used to store not only basic types, it can also be used to store class type :

. 1  class Per {
 2    ****
 . 3  };
 . 4  
. 5  const  int size = 2 ;
 . 6  Per Pers [size];
 . 7  Per P1;
 . 8  Per P2;
 . 9 Pers [ 0 ] = P1;
 10 Pers [ . 1 ] = P2 ; // then you can get a class object through an array of different content

       3. If the element type is a class, calling the default constructor initializes, if no default constructor, should explicitly call other constructors.

       4. The array can not be directly copied or assignment.

Second, Pointer

       1. Another variable address points to a variable, manipulated variable pointer indirectly points to the object;

       2. The array is operated by a pointer:

1  int ARR [ . 3 ] = { 1 , 2 , . 3 };
 2  int * AP = ARR; // the address assigned to the first AP 
. 3  int * AP = AP1 + 1 ; // pointer is incremented corresponding to the pointers move backward 1, n-bit shift plus n

       3 to null: int * p = 0; or int * p = NULL;

       4.void * pointer, the address may be stored in any type of object, typically for a return value of a function or parameter.

       The difference between the pointer and applications: a reference always refers to an object, and must define a reference initialization, and the pointer can not initialize; assign to a reference value is modified object reference associated with that.

       6. pointer and const:

       (1) a pointer to a const object, such as:

. 1  const  a float pi = 3.14 ;
 2  const  a float * PPI;
 . 3 * ppi = & pi;
 . 4 * ppi = 3.15 ; // phrase error, can not * ppi to change the value of pi const object

       (2) const pointer, the pointer itself can not be modified, such as:

. 1  int I = 100 , J = 200 is ;
 2  int * const pi = & I; // pointer pi can not be modified

       const pointer (3) to a const object

. 1  const  Double PI = 3.14 ;
 2  const  Double * const pi_p = & PI; // both here can not be modified

 

Guess you like

Origin www.cnblogs.com/jiang-021/p/11519677.html