Really understand the C language level two pointers

Source: Internet
Author: User

Pointer is the soul of C language, I think for a first-hand people should be very familiar with, and often used: for example, the processing of strings, function parameters, "value, results," and so on, for two-level pointers or multi-level pointers, I would like to understand is relatively easy, such as a two-level pointer to the pointer .... N-level pointers are ....

P *p **p
---  ---  ----
| |->| |->| |
----| |
| |
----

But perhaps it is not easy to understand is that the level two pointer or multi-step pointer where? How to use it? Is there any need to use it?

Now i'll talk about the common use of the C-pointer comparison:

We all know that the function pass parameter in C is passed "value", as Follows:

void Fun (void)

{

int tmp = 0;

Change (tmp);

printf ("################ tmp =%d/n");

return;

}

void Change (int Tmp_t)

{

tmp_t = 1;

Return

}

This time fun () print out the TMP value is still 0, because we pass the "value", if you want to modify the function in the change () the value of the TMP can be in fun (), then you need to pass the pointer to the Following:

void Fun (void)

{

int tmp = 0;

Change (&tmp);

printf ("################ tmp =%d/n");

return;

}

void Change (int *tmp_t)

{

*tmp_t = 1;

Return

}

This time fun () print out the TMP value is 1, because we pass in at this time is the TMP address, so we in the change () tmp_t is the address of tmp, and for *tmp_t operation is actually the operation of the Tmp.

When we get here, we can imagine that we're going to change a value by passing pointers, so when you need to modify a pointer, we need pointers to the pointer, as Follows:

int fun (void)

{

int *buf;

int ret;

ret = Mem_init (&buf);

Return ret;

}

int Mem_init (int **buf_t)

{

*buf_t = malloc (100);

Return 1;

}

As we can see from above, the fun () function is implemented by calling the Men_init () function to allocate memory space to the BUF. First BUF is a pointer to our definition, &BUF is a pointer to buf (level two pointer), we pass the &buf a men_init () function, then the level two pointer buf_t=&buf, so that buf_ T is a pointer to buf, then for *buf_t operation is actually the operation of buf, so fun () can be men_init () to allocate memory.

(add one point: for a defined int **buf_t, level two pointer buf_t=&buf, point to BUF (or a pointer), first-level pointer *buf_t=buf, point to *buf,

Value **buf_t= *buf)

The use of n-level pointers is pretty much the Case.

This is my understanding of a little, if there is not, I hope you have a lot of guidance.

Really understand the C language level two 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.