Pointer and array in C Miscellaneous (1)

Source: Internet
Author: User
Mind Map

Introduction 1> pointer definition: A pointer is a variable that saves the variable address. 2> focus> relationships between pointers and arrays> rules for manipulating pointers 3> advantages of pointers> the only way to express a calculation> Code More efficient, more compact 4> pointer disadvantages: hard to understand, but the code will be very clear when used. 5> integrating arithmetic operations of pointers, arrays, and addresses is a major advantage of C language. Pointer and address 1> Memory Organization Mode

(1) memory is composed of units. Each memory unit stores the binary information of one byte (8 bits. (2) memory units in the machine are ordered. (3) The Machine specifies different addresses for each memory unit to manage the memory. In this way, the CPU uses addresses to identify different memory units and correctly operate the memory units. 2> relationship between pointers and variables (P: pointer variable, C: Memory Object)

>>> P: Save the first address of the unit in C: -- the address here is not a physical address, but a virtual address after address ing, that is, a logical address.

>>> P: pointer to C:

3> understand pointers

>>> Memory space occupied by the pointer: 32-bit system occupies 4 bytes, and 64 is 8 bytes.

Machine Configuration:

Print pointer size:

>>> Pointer is the address. We can think of the pointer as the data type used to store the address. You cannot simply treat a pointer as an integer, although the address value is an integer.

>>> Pointers are of the type, but they are not used to allocate memory to pointers, but to address pointers.

 

Pointer and function parameter 1. Common parameter: the C language passes the value to the called function by passing the value. > The variable value is copied to the called function. > Copy: the value of the variable is assigned to a new variable (parameter). The variable and the new variable must have the same storage capacity. > The called function cannot be modified. Main callProgram Because the called function uses a copied memory unit. 2. pointer parameters: essentially, they are the same as common parameters, and variable replication is also performed, but the passed value is the address. The called function can access and modify the values of variables in the main program through the address. 3. Common Parameters consumed in memory: dependent on the declarative type. CHAR: 1 byte; short: 2 byte; long: 8 byte pointer parameter: The address stored in the pointer variable (usually 4 bytes-32 bits ), always a fixed length, no matter what type of pointer. -- Unless the processor is not 32-bit. 4. Double * DP, atof (char *) Where DP is a pointer variable, while atof is a function

 

Pointer and array 1. pointer operation array is faster than subscript Operation Array 2. array space allocation. for example, int A [10]; -- 40 adjacent memory units are allocated in the space (10*4 ).

3. pointer Operation Array
 
Int * pA; Pa = & A [0];

 

4. pointer Movement
 
Int* PA;
IntA [10];
Pa = & [0];
PA + 1 points to the next element a [1]: > Memory changes: "pointer plus 1" will move four memory units according to the int type specified by the pointer. In fact, the four memory units are not moved, but pa + 1 is equal to 5th memory unit addresses -- the size of 1 in "pointer plus 1" depends on the type int of PA, The pointer type determines the length of the pointer across memory units. > Pa + 1 points to the first memory unit of a [1], which is 5th memory units.

5. Rules:> & A [I] And a + I share the same meaning and use each other. A + I is the address of the I-th element after. > The array name represents the address of the first element of the array. Address operator 1. pointer initialization: 0 or expression indicating the address. 2. The size of "1" in "pointer plus 1" scales proportionally according to the Data Type length. If the int type occupies 4 bytes of storage space, the corresponding 1 value is 4 times.

 

Verification:

>>> If the memory address of P pointing to the char pointer is 0x000000, the address after p + 1 is 0x000001.

The verification process is as follows:

Running result:

 

>>> If the memory address of P pointing to the int type pointer is 0x000000, the address after p + 1 is 0x000004.

Running result:

 

3. No arithmetic or comparative operation is defined between pointers pointing to elements of different arrays.

 

4. pointer subtraction: If p and q point to the elements in the same Array and P <q, Q-p + 1 is the elements (including p and q) between p and q)

Code Verification:

Running result:

Process changes: Q-P = 16 => 16/4 = 4 (scaled according to memory units occupied by the int type) => 4 + 1 = 5;

 

Summary C Language Aspect pointer, because the PHP kernel files were read these two days, due to lack of C, it was very difficult to look. So I want to review the C language. Why do we start with pointers? It may be because pointers are difficult in C. So first, take the hardest part. I wanted to write it all at once, but the pointer has too much content, so I decided to write it in several batches. I am still weak in C language, if Article There are errors. Hope you can give me some advice. I know that there are many experts in blog center C. I am a cainiao-level developer with no development experience, so I hope experts can give me more advice!

 

 

 

References: C Programming Language

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.