Really understand the second-level pointer of C language, second-level pointer of C Language

Source: Internet
Author: User

Really understand the second-level pointer of C language, second-level pointer of C Language

Pointers are the soul of the C language. I think you should be familiar with the first-level pointers. They are also frequently used: for example, string processing, "value, result passing" of function parameters, etc, I want to understand the second-level pointer or multi-level pointer, for example, the second-level pointer is the pointer to the pointer ..... the n-level pointer is ....

However, it may not be easy to understand where second-level or multi-level pointers are used? How to use it? Is it necessary to use it?

Now let's talk about the common usage of the C pointer:

We all know that in C language, function parameters are passed with "values", as shown below:

Void fun (void)

{

Int tmp = 0;

Change (tmp );

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

Return;

}

 

Void change (int tmp_t)

{

Tmp_t = 1;

Return;

}

At this time, the tmp value printed in fun () is still 0, because we pass the "value". If you want to change () in the function () to modify the tmp value in fun (), you need to use a pointer to pass the following:

Void fun (void)

{

Int tmp = 0;

Change (& tmp );

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

Return;

}

 

Void change (int * tmp_t)

{

* Tmp_t = 1;

Return;

}

At this time, the tmp value printed in fun () is 1, because we passed in the tmp address, so in change (), tmp_t is the tmp address, * tmp_t operations are actually tmp operations.

At this point, we can think about how to modify a value by passing a pointer. When you need to modify a pointer, in this case, we need 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;

}

 

Through the above, we can find that the fun () function allocates memory space to the buf by calling the men_init () function. First, buf is a pointer we define, and & buf is a pointer to buf (second-level pointer). We pass & buf A men_init () function, now the second-level pointer buf_t = & buf, so buf_t is the pointer to the buf, so the * buf_t operation is actually an operation on the buf, so fun () you can use men_init () to allocate memory.

(Supplement: For the defined int ** buf_t, the second-level pointer buf_t = & buf points to buf (or a pointer), and the first-level pointer * buf_t = buf, point to * buf,

Value: ** buf_t = * buf)

The usage of n-level pointers is similar.

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.