Pointer variables are also called to pass values (c )!

Source: Internet
Author: User

Let's take a look at the following code:

# Include <stdio. h>
# Include <stdlib. h>

Void F (int * PI)
{
Pi = (int *) malloc (sizeof (INT ));
}

Main ()
{
Int * Pi = NULL;
F (PI );
Printf ("% d/N", Pi = NULL );
}

If you expect function f to help you change the PI value, you will be wrong. Run the above Code and you will find that the output is 1. At the beginning, I was surprised. Didn't I pass the pointer in? Why didn't I keep the changed results?

Take a look at the following code:

# Include <stdio. h>
# Include <stdlib. h>

Void F (int * PI)
{
* Pi = 5;
}

Main ()
{
Int I = 0;
F (& I );
Printf ("% d/N", I );
}

The value of I is changed by function f when the code is run this time. By comparing the two sections of code, you will find that the real parameters passed to function F in the second section are copies of an integer variable address, and function f obtains this address, the variable pointed to by this address is changed. This will certainly succeed. The address is the same as the copied value, so the variables found according to the two addresses are naturally the same. The first code is completely different. Although the real parameter passed to function f is also a copy of the integer variable address, function f tries to change the original address value by changing this copy, which of course won't work, because the pointer and its copy are two variables. A pointer is also a variable, which is also called by passing values during function calls. In fact, the first code is similar to the following code:

# Include <stdio. h>
# Include <stdlib. h>

Void F (int I)
{
I = 5;
}

Main ()
{
Int I = 0;
F (I );
Printf ("% d/N", I );
}

In this code, we can see that function F does not change the value of I, because the main function passes only a copy of the variable I to F, changing the copied value does not affect the original variable value. In fact, what is the difference between this code and the first paragraph? But one is an integer variable and the other is a pointer variable. They are all called by passing values.

So how can we modify the first code so that it meets our requirements? In fact, you only need to let function f simply return the address:

# Include <stdio. h>
# Include <stdlib. h>

Int * F ()
{
Return (int *) malloc (sizeof (INT ));
}

Main ()
{
Int * Pi = NULL;
Pi = f ();
Printf ("% d/N", Pi = NULL );
}

I have translated a short article to avoid returning a pointer to a local variable. The content is not in conflict with this article. The above code is in line with the first case in that Article. We need to avoid the second case.

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.