C ++ day05 Study Notes

Source: Internet
Author: User

1. Storage of Variables
(1) memory is a piece of space. Each byte is numbered, so that the computer can find data by serial number in the future.
(2) addressing method: absolute addressing (in the wholeProgram), Relative addressing (the byte offset from the logical 0, used in the process)

2. Get the variable address
(1) "&" & I indicates taking the address of I in the memory
Address addressing is represented in hexadecimal notation
(2) logic 0 inCodeZone
The global variable is in the Data zone, and the address addressing is greater than 0
Local variables are in the stack, and the address is smaller than 0.

3. array and structure address
(1) the data in the array is continuously stored in the memory. The address difference of each element in the array should be the size of the array element type.
(2) structure address:
The structure space is continuous.
The starting address of the structure is the same as the address of the first member variable.

4. Storage address-
pointer: storage variable address
the pointer type is determined by the variable type of the address to be saved
int * can only store the address of the int variable
the pointer assignment must be a pointer of the same type. in order to assign values to each other!
5. pointer Operations
(1) operations between pointers
"+", "*", the "/" pointer and pointer cannot perform these operations, which is meaningless!
"-" can be used for subtraction. "sizeof (pointer type)" is used as the calculation unit! Note: Only pointers of the same type can be used for this operation. If they are different, the operation unit is ambiguous.
(2) operation between pointer and number (plus or minus)
int I = 100;
int * P = & I;
Print p + 1-> It is equivalent to adding 4 to the address, because the stored variable is of the int type.
P + 2-> It is equivalent to adding 8 to the address.

6. Access the directed variables through pointers
* P indicates the variable pointed to by pointer p * P <=> I

When the pointer is declared, it is initialized.
Int * P = NULL; indicates that the pointer is not explicitly pointed to. * P is not allowed. A "segment error" exception occurs --> NULL pointer

Segment error cause (1) NULL pointer
(2) array out-of-bounds
(3) The recursive conditions are incorrect.

7. classroom exercises
Print the values of elements in the array with pointers.

# Include <iostream> Using   Namespace  STD;  Int  Main (){  Int Ai [6 ] = { 34 , 4 , 12 , 67 , 34 , 2  };  Int * P = & Ai [ 0  ];  For ( Int I = 0 ; I < 6 ; I ++ ) {Cout < "  A [  " <I < "  ] =  " <* (P + I) < Endl ;}  Return   0  ;} 

 

Int* P =AI; the essence of arrays is implemented by pointers. The array name represents the first address (Starting address) of the array. The array name is directed to the first address ([0]) Pointer to the AI array name, which is the pointer to the first address of the array. You can use subscript to retrieve elements, or use array name as a pointer to access elements.* (AI +N) P pointer name. It is also a pointer to the first address. You can also use a subscript (like an array name) to access the array element P [N].<=> * (P + n)

8. Structure pointer

    Struct  Person {  Int  ID;  Int  Age ;}  Int  Main () {person per = {1 , 20  }; Person * P = & Per; cout < "  Per. ID =  " <Per. ID <Endl; //  Get member variables by structure name Cout < "  Per. Age =  " <Per. age < Endl; cout <"  ======================================  " < Endl; cout < "  (* P). Id =  " <(* P). ID <Endl; //  Access the member variables of the structure through pointers Cout < "  (* P). Age =  " <(* P). Age <Endl; // (* P). ID <=> P-> ID can only be used as a structure pointer. Cout < "  ======================================  " < Endl; cout < "  P-> id =  " <P-> id < Endl; cout < "  P-> age =  " <P-> age <Endl;  Return   0  ;} 

9. pointer address
The pointer variable occupies 4 bytes in the memory (it has nothing to do with the type, because the pointer to the saved address only saves the address)
Save the int pointer (int * P = & I) Address and use int ** to save it (INT ** pp = & P)

# Include <iostream> Using   Namespace  STD;  Int  Main (){  Int I = 0  ;  Int * P = &I;  Int ** Pp = & P; cout < "  & I =  " <& I < Endl; cout < "  P =  " <P < Endl; cout < "  & P =  " <& P < Endl; cout < "  Pp =  " <Pp < Endl; cout < "  & PP =  " <& PP < Endl; cout < "  I =  " <I < Endl; cout < "  * P =  " <* P < Endl; cout < "  * PP =  " <* PP < Endl; cout < "  ** Pp =  " <** Pp < Endl;  Return   0 ;} 

Execution result:

 
& I =0 xffbffbecP=0 xffbffbec& P =0xffbfbe8PP=0xffbfbe8& PP =0xffbffbe4I=0* P =0* PP =0 xffbffbec** Pp =0

PP-> P-> I-point relationship
Pp = & P = & I
* PP = p * P = I ** pp = * P = I

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.