Pointer --- C language soul, pointer --- C language soul

Source: Internet
Author: User

Pointer --- C language soul, pointer --- C language soul

When I was a beginner in C language, many of my friends would inevitably stay in the pointer field for a long time, including myself. I can't let it go for a long time and love and hate it. Calm down and I want to summarize my experiences when I was learning the pointer. I am also the first time I wrote a blog. I am very excited. I hope my friends can correct me and criticize me more!

First of all, for pointers, we will give a beginner's friend the most perceptual knowledge: the so-called pointer refers to a volume, a storage content is a volume of addresses, this concept includes two points:

1. the pointer is a volume, corresponding to a memory area;

2. pointer storage information is the address of a memory unit.

 

As shown in, to store 32-bit address data, the pointer occupies 4 bytes and each byte has 8 binary digits. the pointer stores the address data of a certain type in the memory, here we use the double type as an example. The double type data occupies 8 memory bytes and stores the address of the first byte in the pointer. In this way, the double data can be accessed through the pointer. This is similar to the address and business card in real life. It is used to print the address on the business card. Here, the business card has the same role as the pointer and stores the address data.

Obviously, when we introduce pointers, we mention memory many times. Naturally, let's talk about some memory concepts first!

  About memory

As we all know, memory is a concept that is frequently used. In terms of hardware, memory is a physical device, and in terms of functions, memory is a data warehouse, the program must be loaded into the memory before it can be executed by the central processor (CPU.

1. Memory in the computer

Taking Windows as an example, executing a program installed on the hard disk actually reads the instruction and data of the program into the memory for the CPU to execute. Memory is composed of a series of storage units numbered sequentially. In the memory, each storage unit is a unique address, and the address can be used to conveniently access information in the memory unit. The data in the memory depends on the power supply. When the computer is shut down or accidentally powered off, all the data will disappear forever.

2. Memory Address

The introduction of the memory address is the same principle. to access each memory unit correctly, we need to address it. Taking a 32-bit computer as an example, the address space is 32-bit, 32 is the address encoding, such as 0X87654321. The memory address is continuous, and the address difference between adjacent memory units is 1. memory can be regarded as a flat and continuous one-dimensional space.

3. stored content in memory

In a computer, binary data is stored. The size of each memory unit is 1B, that is, 8 bits. Memory is the only large-capacity device that can be directly accessed by the CPU. If you use Windows, you know that you double-click an executable program and the CPU will execute it. This is actually a complicated Memory loading process:

① Load the corresponding code to be operated by the program to the Code Area

② Load global and static data to the data Zone

③ Open up stacks and use temporary variables

4. Memory and Operating System

Before running the program, you need to apply for storage space from the operating system. When the memory space is idle enough, the operating system will allocate a memory space and copy the software in the memory to the memory, and start the software running. During the running period, the space allocated by the software is no longer allocated to other software. When the software is running, the memory space of the software will be reclaimed (but it is not clear that the remaining data, java local variables must be initialized for this reason to prevent being affected by the legacy data)

After pulling so much memory content, I just tried to make beginner friends feel a little bit. I also gave the learning Pointer a try!

  Pointer

The pointer is the address, and the address is the pointer.

The pointer variable is the variable that stores the memory unit address.

The essence of a pointer is a non-negative integer with limited operations.

Let's talk about the pointer in two aspects:

1. pointer of the basic type

Pointer definition: type * pointer variable name;

For example, int * p; double * q;

1 include <stdio. h> 2 int main () {3 int * p; 4 int I = 10; 5 int j; 6 // j = * p; 7 // p = & I; 8 // * p = I <-> I = I 9 // j = * p; 10 // char ch = 'a '; 11 // p = & ch/* type inconsistency Error */12 // printf ("I = % d, j = % d, * p = % d", I, j, * p); // 10 10 1013 return 0; 14}

Code 3 ~ The five rows define three variables (for example): int *-type variables p, int-type variables I, and int-type variables j. It should be clear that in the third row, p is the variable name, and int * indicates that the variable can only store the address of the int type variable.

 

If you do not know where to direct the pointer at the beginning of the pointer variable declaration, the simplest way is to set it to "0". The C language provides the keyword NULL: int * pInt = NULL;

If you release the 6th line comment. What will happen? An error is inevitable! Because * p stores the address, but the above Code does not assign a value to p. P doesn't know which unit, p points to an uncertain unit, and spam data! If you open row 9th, this will not happen, because the p value is assigned to row 7th, and the p variable saves the I address!

In row 7, there are three sentences:

① P Stores the address value of I, and p points to I.

② But it does not mean that p is equal to I or I is equal to p. Modifying the I value does not affect the value of p, nor does it affect the value of I.

③ What I can represent, * p can represent. (For example, 8th rows)

Let's see 10th ~ Row 11. Is this correct? Define a char character and store its address in the integer pointer p. There is no doubt that this is also wrong. As mentioned above, int * indicates that this variable can only store the addresses of int type variables.

Next, let's look at the role of pointers in functions:

 1 #include<stdio.h> 2  3 void f(int i) 4 { 5     i = 100; 6 } 7  8 int main(void) 9 {10     int i = 5;11     f(i);12     printf("%d\n",i);13     return 0;14 }
1 # include <stdio. h> 2 void f (int * p) // instead of defining a shape parameter named * p, it defines a shape parameter of p, its type is int 3 {4 * p = 100; 5} 6 7 int main (void) 8 {9 int I = 5; 10 f (& I ); 11 printf ("% d \ n", I); 12 return 0; 13}
View Code

In the first code, can I be successful if I want to change the value of I through the function? Obviously, I is a local variable, and the final value of I output is still 5.

So how can we change the value of the main function according to the called function? The second code implements this problem by using the "* Pointer" form, you can directly access the memory space pointed to by the pointer. In other words, because p stores the address of I through parameter transfer, * p is equivalent to I, and "* p = 100" can be indirectly referenced; you can rewrite the area pointed to by the pointer! Note that in the second line of the Code in the second paragraph, the form parameter is int * p. Instead of defining a form parameter named * p, it defines a form parameter named p, its type is int *!

2. Relationship between pointers and Arrays

First, you must understand that the array name is a pointer and points to the first address of the array! The address of the array element is continuous.

Meaning of array name:

① Is a pointer variable

② Stores the address of the first element of the one-dimensional array.

③ Value cannot be changed

④ Point to the first element

The relationship between subscript and pointer:

A [I] = * (a + I)

1 # include <stdio. h> 2 void printArray (int * p, int len) {3 p [2] =-3; 4/* p [0] = * p [2] = * (p + 2) = * (a + 2) = a [2] 5 p [I] is the [I] 6 */7 int I = 0; 8 for (I = 0; I <len; ++ I) {9 printf ("% d \ n", p [I]); 10} 11} 12 int main (void) 13 {14 int arr [5] = {1, 2, 3, 4, 5}; 15 printArray (arr, 5); 16 return 0; 17}

With the above theory, it is hard to understand this code. p [0] = * p; indicates the meaning, because the array name is a pointer pointing to the address of the first element of the array, so the first element p [0] of the array is equivalent to * p!

Because "a [I] = * (a + I)", you can know that p [2] = * (p + 2), because p points to the first element, then p + 2 naturally points to the third element, so p [2] = 3 is to assign values to the third element of the array. This is okay! However, note that * (p + 2) is not the same as * p + 2. The latter adds 2 to the value of * p. The former is not the case, instead, the address "+ 2" is given to him ".

 

Today, we will come here first. There are a lot of pointer content. We should continue to summarize it and discuss it with you later!

I wrote blogs for the first time, but there are a lot of mistakes! Hope you will not be enlightened!

  

 

Related Article

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.