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.