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