C language, pointer and wild pointer Problems

Source: Internet
Author: User

Int _ tmain (int argc, _ TCHAR * argv [])
{
Int * p = (int *) malloc (sizeof (int ));
// * P = 10;
Flee (p );
Printf ("% d", * p );
// Release p
Free (p );
Printf ("----");
Int * I = (int *) malloc (sizeof (int ));
Flee2 (& I );
// Release I
Printf ("% d", * I );
Free (I );
}
// The input parameter is a pointer and is still passed as a value, that is, a copy. Only the reference transfer is to pass p itself.
Void flee (int * q)
{
// The copy modifies the value of the variable referred to by the pointer, rather than the pointer itself.
* Q = 20;
// When the stack is output, the copy is automatically destroyed, and the variable value indicated by P outside is changed, but the pointer address is not actually changed.
}

Void flee2 (int ** q)
{
// Int I = 5;
//// The copy points to a new location, so a pointer address is actually modified. However, one problem is that the original definition of I produces a wild pointer.
// * Q = & I;
// I = 3;
If (q! = NULL & * q! = NULL)
{
* (* Q) = 5;
}
}

 

From kung fu pandatv

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.