Passing parameters with double pointers

Source: Internet
Author: User

Today, I took a moment to think about the problem of using double pointers for parameter passing. I used to only know what to use, but I have never thought about why. Today is just a bit xian.
# Include "stdio. h"

Void fun (char * pa)
{
Printf ("pa value: % d \ n", pa );
Pa = "bb ";
Printf ("pa value: % d \ n", pa );
}

Int main (void)
{
Char * p = 0;
Printf ("p value: % d \ n", p );
Fun (p );

If (p! = 0)
{
Printf ("output p =" after changing ");
Puts (p)
}
Else
{
Printf ("the P value has not changed! \ N ");
}

Return 0;
}

Result output "P value unchanged! ", It is easy to understand: When passing parameters to the fun function, there should actually be an invisible operation pa = p. This operation is the same as the assignment of two common variables, the p value is assigned to pa,
Then, we can see from the printed value that in fun, the pa value is originally the address of p. However, pa points to "bb" in fun, the problem arises. The pa value has indeed changed, and the point has also changed,
However, the p value we want to change is still "0".
Later, I found that the value of p is not changed because pa changes his own value and does not change the value of the address he points, however, in this example, the p initialization parameter is 0, that is, NULL. If * pa = 'B' is performed'
This operation is illegal. In order to make the operation successful, I assigned the initial value "aa" to p, and then the operation was still unsuccessful. Originally, because the computer memory is mainly divided into Stack, heap, static, read-only
Zone, where the stack zone is allocated with local variable space, such as function calls, local variables, code to be executed, and some memory that we do not need to manually allocate or release; the heap area increases upwards, mainly by allocating space requested by programmers. Static partitions are allocated with static variables and global variables. Read-Only partitions are allocated with constants and code spaces. Therefore, this operation will fail ..

Now, let's make a conclusion: first, we need to pass a pointer to change the pointer's pointing value. In the above example, we only pass an address value, therefore, in the following example, we need to pass a complete pointer variable. I pass the address of this pointer variable into it. This is what I understand as a double pointer as a parameter:
# Include "stdio. h"

Void fun (char ** pa)
{
Printf ("pa value: % d \ n", pa );
* Pa = "bb ";
Printf ("pa value: % d \ n", pa );
}

Int main (void)
{
Char * p = 0;
Printf ("p value: % d \ n", p );
Fun (& p );

If (p! = 0)
{
Printf ("output * p =" after changing ");
Puts (p );
}
Else
{
Printf ("the P value has not changed! \ N ");
}

Return 0;
}

OK, pass the pointer p address as a parameter to the fun function, pa = & p; * pa = "bb "; in this way, the value of p is changed to the address of "bb" in the read-only zone...
Experts Do not spray .... Correct the error!
Experts Do not spray .... Correct the error!
Experts Do not spray .... Correct the error!

Thank you!

 

From general mental
 

Related Article

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.