The beautiful legends of the hands

Source: Internet
Author: User
Tags array definition

I. Talking about pointers

Speaking of pointers, do not know how many C scholars are trapped in this mountain, many people say that the array is the most difficult to learn, but did not think the pointer is more difficult than the array. Didn't hear how long it was completely dizzy, what level of pointers ah, two-level hands ah, three-level hands ah, and finally inexplicably out of an n-level pointer, so that a class down completely dizzy. Of course, it also includes some partners with pointers. arrays, functions, structs, and so on, I may be talking about the fur of the pointer, you can also think of the concept, so I want you to look at the line, specifically to think about it.

1. What is a pointer? In fact, it is the address, said the more popular point is the number, because if you need to find someone's home, whether you have to know the number first. This is a very plain truth. Let's take a look at the first level pointers!

int a = 1;int *p = &a;printf ("p =%p\n", p);p rintf ("a =%p\n", &a);

By the code above, we can conclude that the result is the same, and that both equations print the address. It can be concluded that P = &a, which is the meaning of the address. Of course, we can get the value of *p by their two addresses equal to the value of a, do not believe us to see the implementation of the Code.

int a = 1;int *p = &a;printf ("p =%d\n", *p);p rintf ("a =%d\n", a);

This actually can be fully explained that P is by first find the address of a, and then found the value of a. But you must remember that the following printed results are not equal.

int a = 1;int *p = &a;printf ("p =%p\n", p);p rintf ("p =%p\n", &p);

Seeing that the equation is not, two printed addresses are completely unequal. This is actually involved in a concept, that itself P also has its own address, and the address of the other number is completely unequal, when the element is assigned to the pointer, the address of the pointer does not change, it still has its own address, change only it points to the address, who assigns the address to it, it points to who. We're going to look at a situation next.

int a = 2;int *p = &a;p = &a;*p = A;

Always remember the definition when the *p is separate, just the &a assigned to P, is not assigned to the *p, some people always think *p is always together, always do not know how to use separate, so I tell you. The *p is equal to the value of the element you are pointing at when it is not defined, that is, the memory space. Don't get dizzy.

2. The confusion of level two pointers

In fact, I would like to say that since you already know the use of one-level pointers, then you will be afraid of two-level pointers? It's just a pointer to the pointer, it's not so scary ah, when learning to relax the heart is good. Let's take a look at it below.

int a = 2;int *p = &a;int **p1 = &p;printf ("P1 =%p\n", p1);p rintf ("p =%p\n", &p);

In front of you do not understand the use of the first level of pointers, here we take a look. The first print out of the result is certainly the same, why? See **P1 = &p This equation is actually very clear, *p1 point to the address of P, so print out the result must be equal, since *P1 point to the address of P, and P is the address of a, then **P1 is equal to the value of a, so, you look at the code as long as the * * P1 previous p as a bridge just fine, then you really do not understand, then I draw you a similar element in the memory of the distribution map, first look at it.

int a = 2;int *p = &a;int **p1 = &p;printf ("P1 =%d\n", **p1);p rintf ("a =%d\n", a);

  

See above this element in memory address of the distribution map is not difficult to see, first P1 address (the location of the address written above, I only write the tail number, the middle of the not written), p address and a address is not difficult to see, to ask why I would be such division, I say a probably. That is, the first definition of the element allocated in the high-byte memory, and then allocated to the reduction of the size of the place allocated, because it is an integer, so it accounted for 4 bytes. As can be seen from the graph (*p1 points to P's address), (P's point to a address) so naturally (* * P1 points to the address of a) that is the way to find the value of a is equal to 2. So this is the level two pointer, of course, what three-level pointer ah, four-level pointer Ah, n-level hands ah, the principle is the same, you only have to grasp the level two pointer, the others are not a little. Well, then I'll give you a little bit more knowledge.

Two. Various pointer types 1. Array of pointers

Arrays of pointers, pointers and arrays are concatenated together by usage. What does it represent? An array of pointers is actually an array, except that the elements in it are all pointer types, in other words, arrays of pointers that are used to hold a series of pain types, so you can see the code in detail.

int a1 = 1;int A2 = 2;int a3 = 3;int *p[3] = {&a1,&a2,&a3};

This is the pointer array definition and assignment, below I give you how to take the pointer array value, the following code is the pointer array to take the value. I won't say much about how I got it.

int *a = p[0];
2. Array pointers

An array pointer, as the name implies, is a pointer, and the type of the pointer is an array. How to use it, let's take a look at the code.

int arr[3] = {1,2,3};int (*p) [3] = &arr;

Here to tell you a trick, is the array pointer inside *p = &arr, to remember not to ask me why, in fact, they are not equal, but in the C language learning phase just remember this so the array pointer basically is no problem, unless you want to engage in research projects, then it is absolutely useless, Because they are not equal in nature.

3. Pointer functions

pointer function, function, that means it must be a function, the preceding pointer simply means that the return value is a pointer. What is the specific format, we look at the code is how to write it, may write a little short, I hope you don't mind ah.

*add (int *a) {code block; return &a;}

This is the shortest pointer function, it can be seen that when the function is called, the type of the return value is a pointer type, many times it is used in the main function directly to the address of the argument into the parameter, the address to a and then in the specific operation. This is the pointer function, let's take a look at the last pointer type.

4. Function pointers

The last one is the function pointer, see the last two words to know is a pointer. Collectively, the function also opens up a space in memory, where the function pointer is the address of the space. Understand, is that after the function is defined, the memory is not used before it will allocate space, only in the use of the time will have its own space, and the pointer in the function pointer is the address of this space. This is true of the specific code implementation.

int add (int a) {code block;}int (*p) (int) = add;

Then attach the function pointer to the specific how to pass the value bar, see the code below.

P (3); Add (3);

P (3) can be equivalent to add (3).

Three. Concluding remarks

function I say here almost, whether you understand, I hope this short question can help you, help you answer the questions in mind, follow-up I will also share the knowledge about pointers to everyone, it is my return to you, if there is nothing to understand, you can leave a message to me, I will try to answer ...

The beautiful legends of the hands

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.