Let's talk about the C language pointer.

Source: Internet
Author: User

Let's talk about the C language pointer.

The content I can write is still very small. Please forgive me for its limited level. I hope you can correct me.

 

Pointers have plagued countless programmers. Maybe your teacher will tell you that pointers are hard to learn.

However, don't be scared. The baby was stunned by the teacher's words at the time, so he was not in the mood to attend lectures when learning the chapter. (Just like I heard something else, just kidding)

Lead to various slots when learning the linked list.

 

*********** ***************

 

Data in a program is often used as a variable. Each variable corresponds to several storage units, and the value of the variable is stored in the storage unit, by referencing and assigning values to variables, you can use or modify the data stored in the storage unit.

-- C language programming and practices

If the address of a variable is stored in another variable, the variable that stores the address is called the pointer variable.

The pointer is the address, and the pointer operation is to obtain the data stored under the address.

A level-1 pointer is commonly used, and a level-2 pointer is rarely used.

 

*********** *************

 

For example, ask you to go to the teaching building A214 to find a person a. A214 is the address of the person, and A214 is a level-1 pointer. If you have this address, you can find the person.

Similarly, the second-level pointer is: A214 does not have this person in the classroom, but there is another address on the door: A215. Then you come to A215 and find this person. Then A214 is a second-level pointer.

Because you have to go through two searches to find this person.

 

********** ***************

Pointer declaration and initialization:

1 // type 1: declare and initialize 2 3 int a = 3; 4 int * p = a; 5/* define the pointer Variable p as the pointer to integer data, this sentence is also true if it is written as int * p = & a. It means that * indicates that P is a pointer instead of a pointer operation.
However, in the DEVC ++ environment, there is a warning when writing (not writing & Symbol) like the fourth line of code. */6 7 // second type: declare only 8 9 float * p; // define a pointer variable p10 11 pointing to single-precision data // write a few lectures, the general format for defining a pointer variable is: 12 // <type> * <variable identifier> 13 // variable identifier, just as the above p14 // pointer is in the format of pointer

 

 

A simple piece of code

1 # include <stdio. h> 2 int main () 3 {4 int x, y; 5 int * px, * py, * p; 6 px = & x; // & is the address symbol 7 py = & y; 8 x = 21; 9 y = * px; 10 // pointer operation, that is, the value of the px address (that is, the address of x) is assigned to y11 12 p = & y; 13 printf ("% d, % d, % d \ n ", * px, * py, x, y, * p); 14 return 0; 15} 16 17/* output result: these are respectively 21 obtained by pointer operation on px, 18 21 obtained by pointer operation on py, 21 of x, 21 of y, perform pointer operation on p to obtain the value of y 21 */

 

Note:

1. the pointer cannot point to an unspecified variable, which is also the reason for the existence of the first line of code.

2. the pointer cannot point to a specific value, for example, int * p = 3. This is incorrect.

3. identify whether the current pointer is performing pointer operations or whether the pointer points to a variable.

 

Article 3 I personally think it is very important. Otherwise, I will not be able to understand many things. I don't think it is a pointer operation as soon as I see the number * (that is, the value corresponding to the address is given ), sometimes it only indicates the pointer identity of the variable, and the table points to it.

 

* **************************** Pointers and arrays ******** **********************

 

The array name is its first address, which cannot be changed and can be converted to a pointer.

1 int a [3]; 2 for (int I = 0; I <3; I ++) 3 {4 * (a + I) = I; 5 // * (a) = I; a ++; this write is incorrect. a is a pointer constant and the constant cannot be modified. 6}

 

********* **********************

 

For example, int * const pa =;

Take a closer look. * indicates that pa is a constant before const.

1 int a = 1; 2 int B = 3; 3 int * const pa = & a; 4 a = B; // This sentence can also be replaced by * pa = B. // obviously, if you write pa = & B, because the pointer to pa is a constant pointer 6 printf ("% d", * pa); 7 // The output result is 3

To put it bluntly, you cannot write pa = xxxx, But you can write * pa = xxxx.

 

* **************************** Constant pointer ********* *******************

 

For example, int const * pa =;

Here int and const are the same as before and after

1 int a = 2; 2 int B = 3; 3 const int * pi = & a; // you can see that pi is a pointer to a constant 4 pi = & B; // note that pi can assign a new memory address 5 B = 30 at any time; // If * pi = 30, is Wrong 6 printf ("% d", * pi); 7 // The output is 30

Some people may wonder why the pointer pointing to a constant has changed from 2 to 30?

Okay, because the address of the pi storage can be changed. In fact, it's just that * pi = xxxx cannot be written, but it can be changed in other ways.

Compare the code at both ends of the above, you should understand.

 

* ********************** Why pointer variables should be classified into different types ********** ************

 

Some people wonder if the initialization Pointer Points to a declared variable, is it possible not to write the pointer type? Is the pointer type the data it points?

 

1 int a = 3; 2 char ch = 1; // If '1' is entered here, 49 will be output because it is an ASCII code, the character 1 in the ASCII Code corresponds to the decimal 493 4 char * p = & ch; 5 printf ("% d", * p );

Remember: the pointer type determines how many bytes the pointer will read. (I have not understood how to read, from low to high, from high to low, whether data storage is from high to low, or from low to high)

Would like to know friends can be here to see a deeper understanding of the big write data variables in the memory of the storage method: http://blog.sina.com.cn/s/blog_abc091cc0101h0a3.html

Stack area, stack area, static storage area details: http://my.oschina.net/lxrm/blog/513794

The declared type is more secure.

P reads a byte of data, that is, the content of ch: 1

However, if you change the fourth row to int * p = & ch, the output is not necessarily 1. This depends on the memory allocation, this pointer will read 4 bytes (the int type occupies 4 bytes in 32-bit platform) of data, which is very dangerous.

 

* ************************* Use of pointers in functions ********* ****************

 

See the following code.

Value transfer:

1 # include <stdio. h> 2 3 4 void fun (int a1, int b1) 5 {6 int temp = a1; 7 a1 = b1; 8 b1 = temp; 9} 10 11 int main () 12 {13 int a = 3; B = 4; 14 fun (a, B); 15 printf ("% d", a, B); 16 return 0; 17} 18 19 // output result: 3 4
// A1 = 4, b1 = 3, a = 3, B = 4

Observe this function. It is concise and clear, that is, to exchange two parameters. But why didn't the subsequent output exchange the values of a and B?
This is the case: When passing parameters to a function, a value is implicitly assigned to the parameter in the brackets that call the function.

Fun (a, B); // a1 = a; b1 = B;

After entering the function, the operands are a1 and b1 after being assigned values. Therefore, we only exchange a1 and b1 after being assigned values by a and B, instead of a and B.

This is a common mistake made by function learners for the first time.

 

Look at the next code
Address Transfer:

1 # include <stdio. h> 2 3 void fun (int * pa, int * pb) 4 {5 int temp = * pa; 6 * pa = * pb; 7 * pb = temp; 8} 9 10 int main () 11 {12 int a = 3, B = 4; 13 fun (& a, & B); 14 printf ("% d ", a, B); 15 return 0; 16} 17 18 output result: 4 3

The address of a and B that is passed to the function as the parameter, pa = & a, pb = & B. The value obtained from these two addresses is * pa, * pb is of course, b.

Then exchange, so the exchange is successful.

 

Reference transfer:

Void fun (int & a, int & B) {int temp = a; a = B; B = temp;} int main () {int a = 3, B = 4; fun (a, B); printf ("% d", a, B); return 0 ;}// running result: 4 3

// This code is in the DEVC ++ environment (this is also true for codeblock. I have not tried it in other environments, but it may all happen. c instead. cpp file. If you use the reference transfer function, an error is reported because the reference transfer strictly does not belong to the C language content.


The second part of the three sections of Code can be considered as a simulated reference transfer in C language.

 

* ****************************** Pointer to the function ***** *************************

 

The memory address of a Data variable can be stored in the corresponding pointer variable, and the first address of the function can also be stored in a function pointer variable, in this way, you can use this function pointer variable to call the function pointed.

Function pointer variables are the same as other variables and must be declared before they can be used. Similar to the void fun (int) Declaration of the function, the pointer to this function must be written as void (* pfun) (int)

So: void fun (int );

Void (* pFun) (int );

The two rows complete the declaration of a function pointer.

 

1 # include <stdio. h> 2 3 void fun (int x) // if this is just a declaration, do not write the implementation, x can not be written, write only the parameter data type 4 {5 printf ("% d \ n", x); 6} 7 8 int main () 9 {10 void (* pFun) (int ); // int x11 pFun = & fun; // set the pFun function pointer to fun function 12 fun (10); // directly call function 13 (* pFun) (20); // call the function 14 return 0 through pointer; 15} 16 17 // running result: 1018 20

 

Here, I hope that people who do not understand the pointer will feel that the pointer is not that difficult to understand. Although I write a very small level of content, please forgive me for its limited level.

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.