C ++ pointer early learning

Each variable has a memory location, each memory location addresses are defined using a hyphen (&) operator access, it represents an address in memory.

#include <the iostream> the using namespace STD; int main () 
{ int   var1;
    char var2 [ 10 ]; 
   COUT << " var1 variable address: " ; 
   COUT << & var1 << endl; 
   COUT << " address variable var2 : " ; 
   COUT << var2 & << endl; return 0 ; 
}

 


   



    

result:

Address var1 variable: 0x28fefc 
address var2 variables: 0x28fef2

What is a pointer?

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like before, like other variables or constants, you must use other variables to store address pointers, it is declared. The general form of the pointer variable declaration is:

type *var-name;

Here, type  is a base type pointer, it must be a valid C ++ data types, var-name  is the name of a pointer variable. Used to declare a pointer asterisk * asterisk used in multiplication are the same. However, in this statement, the asterisk is used to specify a variable is a pointer.

int     * IP;     / * a pointer integer * / 
double * DP;     / * a type double pointer * / 
a float   * FP;     / * a pointer to float * / 
char    * CH;     / * A string pointer * /

All pointers actual data type value, whether integer, float, string, or other data types are the same, is a long hexadecimal memory address of the representative . The only difference between the different types of data pointers, the pointer points to a different constant or variable data type.

Using pointers in C ++

Frequently performed using the following pointers: Define a pointer variable, the variable is assigned to the address pointer, the value of the available address in the access pointer variable. These by using unary  *  operand Returns the value of the variable of the specified address. The following example relates to these actions:

#include <the iostream> the using namespace STD; int main () 
{ int var = 20 is ;    // declare real variables int   * IP;         // declare a pointer variable 
   * IP = var ;        // address stored in the pointer variable var 
   COUT << " the Value of variable var: " ; 
   COUT << var << endl; // output variable stored in the pointer address 
   COUT << " the address IP variable stored in: " ; 
   COUT << << IP

 


     
   



   endl; 

   // access the address pointer value 
   COUT << " the Value of variable * IP: " ; 
   COUT << IP * << endl; 

   return  0 ; 
}
Value of var variable: 20
Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20

Pointer types

int * ptr;  
char * ptr;  
int ** ptr;  
int (* ptr) [3];  
int * (* ptr) [4];  

  From a linguistic point of view, as long as you move the pointer in the pointer declaration statement name removed, the rest is the type of the pointer. This is a pointer type itself has. Let us look at the various types of cases in a pointer:  

int * PTR; // pointer type * int   
char * PTR; // pointer type * char   
int ** PTR; // pointer type ** int   
int (* PTR) [ . 3 ]; // Pointer of type int (*) [. 3]   
int * (* PTR) [ . 4 ]; // pointer is of type int * (*) [4]  

In the arithmetic operation of the pointer, the type of the pointer points to a significant role.  

Pointer type (i.e. type pointer itself) and the pointer points to the type of the two concepts

 

Pointer value


Is a numerical value of the pointer stored in the pointer itself, the value will be treated as a compiler address instead of a general numerical value. In the 32-bit program, the value of the pointer of all types is a 32-bit integer, 32-bit program because in all memory addresses are 32 bits long.

The pointer points to the memory area from the memory address is represented by the value of the pointer, length sizeof (type pointer points) in a memory area. Later, we say that a pointer value is XX, is equivalent to saying that the pointer points to an area of ​​memory address XX headed; we say that a pointer to a block of memory area, it is equivalent to saying that this is the value of the pointer the first address of the memory area. 

Type pointer points to the memory area, and the pointer points are two completely different concepts. In one embodiment the type of the pointer points to have, but the pointer has not been initialized, it points to the memory area does not exist, or is meaningless. 

Pointer memory area occupied by itself

The pointer itself accounts for how much memory? You just (type pointer) measured with function sizeof will know. In the 32-bit platform, the pointer itself occupies 4 bytes in length.

The pointer itself occupied memory concept in determining whether or not a pointer expression is an lvalue useful. 

Pointer arithmetic

Pointer can be plus or minus an integer. Meaning as such operations typically pointer and the value of the subtraction is not the same.

char a[20];  
 int *ptr=a;  
...  
...  
 ptr++;  

 

In the example above, the type is a pointer ptr int *, it points to a type of int, which is initialized to point to integer variable a. Next, the third sentence, ptr pointer is incremented by 1, the compiler is addressed: It adds a value of the pointer ptr sizeof (int), the 32-bit program, is added 4. Since the address is made in units of bytes, so the original ptr points to the address of the address of a variable increase of 4 bytes to higher addresses.
Since the char type is one byte length, so that the original ptr is a pointer to the array of unit No. 0 is four bytes beginning at this time point to a four byte array starting from No. 4 unit.
We can use a pointer and a loop to iterate over an array

int Array [ 20 is ];  
 int * PTR = Array;   
...   
// omitted here integer array assignment code.  
...  
 for (I = 0 ; I < 20 is ; I ++ )   
{   
    ( * PTR) ++ ;   
    PTR ++;  

This example of each value of the integer array plus 1 unit. Since each cycle ptr pointer plus 1, so each loop can access the next element of the array.

 https://blog.csdn.net/poison_biti/article/details/53227488

Guess you like

Origin www.cnblogs.com/delongzhang/p/10932407.html