I rely on, pointer!

Source: Internet
Author: User

Since C #. net has been used recently, almost no pointer operations will be performed. In fact, when I first learned C ++, I didn't know how to understand the pointer. Later I didn't use it very much. I simply put it down. Yesterday, I had a postgraduate discussion class and finally got into touch with pointers because the language returned to C ++.

Pointer, I think it's already the most successful thing when computer languages are engaged in memory. But the pointer is really confusing.

When we declare pointers, there are generally two ways to write them. They are nothing more than the space and * positions. For example, if I declare a pointer to an int, I can write it as an int * PTR or an int * PTR. The two statements are recognized by the compiler without any difference, however, it is too difficult for people to understand. In the most intuitive impression, int * PTR declares a variable called PTR and its type is int *. For int * PTR, it declares a * PTR variable, its type is int. In fact, it is good. If you write all the statements and values, the second one looks more reasonable, that is, int * PTR = new int (10); it is used now. net is used more, see the next thing, good, it indicates a 10 int32 struct, but in C ++, if the new thing is used, the returned result is a pointer. Although the pointer is stored as an address, it is an int variable. But if we write it as char * PTR, it cannot be said that an address is returned and saved as char type. Therefore, I personally think that the first method is the most intuitive and easy to understand, that is, int * (the same as int []) is a type that declares a variable called PTR, as a pointer variable of the int * type, this variable must point to an int, that is, its stored value must be an address that stores the int type.

alas, it's awkward to say it, because it's just a declaration, and * is used in Declaration, it's just the beginning, and there will be a pointer operator that will use it later, plus the & find the address operator, there is more fun. As mentioned just now, int * PTR = new int (10); declares a pointer called PTR, which points to a piece of memory space, and this memory space stores an int -- 10. Well, if we use cout

Okay, maybe you will say you know all these things. Maybe so, but it's not over yet. There are other things in the end. Pointers are more fun, for example, pointers. For example, I declare an int ** pptr and assign a value to it. Which of the following can be compiled? New int (10 )? 10? PTR? & PTR? Only & PTR can be edited. This is because int ** pptr declares a pointer, which must point to a pointer declared as int *. That is to say, it is a pointer, it should be: I declared a pointer variable called pptr, which must point to the address where another pointer variable is stored, and that pointer variable must point to an address that stores the int value. Okay, let's look at this int ** pptr = & PTR; if we cout <pptr, what do we get? Yes. It's an address. Who's the address? It is PTR. This sentence is the same result as cout <& PTR. This is what the previous assignment operation did, it stores the address value obtained by & PTR in pptr, but this pptr is an int ** type. Also, if we use cout <* pptr, what is the result? Or the address. This is the address that stores 10, because * This pointer operator is used to retrieve the content stored in the address saved in pptr, the output of this sentence is the same as the result obtained by cout <PTR. So, what is cout <** pptr? This is 10, because * the operation order of this item is from right to left, ** pptr is * (* pptr). I just mentioned that * pptr and PTR are one thing, the ** pptr and * PTR are one thing, so the output is 10.

Well, I don't want to nest it any more. Int *** is actually meaningless, and I figured it out above. It's nothing more than recursion, and I started to become a text game. However, this is not complete yet. The pointer is highly correlated with value transfer and reference transfer. For example, I can write several methods as follows:

1 Void F ( Int * PTR)
2 {
3 PTR =   New   Int ( 10 );
4 }
5
6 Void F ( Int I)
7 {
8 I =   10 ;
9 }
10
11 Void F ( Int ** Pptr)
12 {
13 * PTR =   New   Int ( 10 );
14 }

I write int X in the main function. Then I want to call the F method to assign values to X. Which one can be useful? First, let's take a look at the first one. I call f (x). Then X is passed by value. Because int type does not assign a value to 0, this sentence is similar to F (0) there is no difference, because in F (int I), the form parameter I is changed to 10, and that passed 0 has nothing to do, so it will be pop out of scope, so after the call, X is still 0. Let's look at the F (int * PTR ).CodeAnd the result.

1 Int * PTR =   New   Int ( 0 );
2 Int X =   * PTR;
3 F (PTR );

In this Code, I initialized a PTR pointer and pointed it to 0. Then I gave it to X, and then I called F (PTR ), what is X after that? Some people say that this is a nonsense. X must be 0. You have given the previous value to X before calling F (PTR). How can we change it after PTR? X will not change. Yes. What if the code is like this? Code

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> int * PTR = New int ( 0 );
F (PTR);
int x = * PTR;

I didn't make a mistake this time. I just assigned the value to X after calling F again. This time, X should have gotten 10. Unfortunately, X is still 0. Although I have a pointer instead of an int value, the pointer is also a value. In F, I just copied the value of this pointer to the past, that is, in F, there is a new pointer. Like the PTR outside, it points to the 0 address. Then, inside f, we point this pointer to 10, the PTR outside still points to 0, so the X is still 0.
The Code is as follows: Int * PTR =   New   Int ( 0 );
F ( & PTR );
Int X =   * PTR;

This time I passed the PTR address as an int **. In F, I first requested * pptr, which means I got the PTR. This is not a copy, this is the real PTR. When passing the value, it copies the & PTR address, that is, the PTR address. However, after a * pointer operation, I have obtained the PTR, then I set PTR = new (10); so the X outside is changed to 10.

I have learned a lot about pointers. I remember my major questions during my postgraduate exams, AS WELL AS array pointers and pointer arrays. For example, if I write int * [] and INT [] *, what do these two items represent? In fact, if you understand all int * and INT [] as a type, this is not so troublesome. Int * [] is what, first [] is followed by an array, which stores all int * type variables, so it is a pointer array; what is int [] *? First, * is followed by a pointer pointing to an array of int []. Therefore, it is an array pointer.

There are so many things I can think. net is good. When do you want to pass it by reference? Just add a ref to it and you will be done. It's a little worry. Which of the following is a reference like C ++, annoying. However, I think this abstract and logic is a good practice, just like playing a combination of mathematics...

in short, what I want to write is helpful for beginners who want to learn pointers.

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.