I would like to summarize the difficult points in C/C ++ raised by many people:
The key to poor pointer learning is unclear concepts. To put it simply, books are not carefully read.
The learning of needles is just like a person learning to repeat passwords without reading more, learning more, and practicing more. The following are two classic examples, which are available in many books, the learning focus is on understanding * X and X. They are different. * x
The expression is actually the variable A itself, and X represents the address of variable A in the memory. If you want to understand it, you can output and observe cout <* X "|" X ;, if int is defined
* X; followed by an understanding of X = &. Read carefully and contact the following two examples. I think the pointer problem is not difficult!
# Include <stdio. h>
Main ()
{
Int A, B;/* defines two integer variables A and B */
Int * point_1, * point_2, * temp_point;/* defines three pointer variables */
Scanf ("% d, % d", & A, & B);/* format the value of A and B */
Point_1 = & A;/* point the value of the pointer variable point_1 to the address of variable */
Point_2 = & B;/* point the value of point_2 to the address of variable B */
If (A <B)
{
Temp_point = point_1;/* Here, temp_point is used to temporarily store the value of point_1, that is, the address of variable */
Point_1 = point_2;/* assign point_2 to point_1 */
Point_2 = temp_point;
/* The point_1 value cannot be found because it has been changed. Use the temporary storage method temp_point to retrieve the original point_1 value and assign it to point_2 to change the point_1 and point_2 values */
}
Printf ("% d, % d", * point_1, * point_2 ); /* use * point_1 and * point_2 to distinguish between B and A to display the value on the screen */
}
/* Note that this question is correct. This method does not change the variable A. The value of B only uses the pointer variable to store the addresses of A and B respectively, then swap the values of the two pointer variables, which are actually stored in
The address of A and B in the pointer variable is swapped, and * point_1 and * point_2 are used to display the exchanged value. * point_1 is actually, in this example, the algorithm does not actually change the values of A and B,
Use pointers for address switching to sort the size.
*/
# Include <stdio. h>
Main ()
{
Int A, B;/* defines two integer variables A and B */
Int * point_1, * point_2;/* defines three pointer variables */
Scanf ("% d, % d", & A, & B);/* format the value of A and B */
Point_1 = & A;/* point the value of the pointer variable point_1 to the address of variable */
Point_2 = & B;/* point the value of point_2 to the address of variable B */
Compositor (point_1, point_2);/* Call the number of custom sorting Han, and pass the addresses of A and B to point_1 and point_2 */
Printf ("% d, % d", a, B);/* print the values of A and B */
}
Static compositor (P1, P2)
Int * P1, * P2;/* defined format parameter P1. P2 is the pointer variable */
{
Int temp;/* create a temporary storage variable */
If (* P1 <* P2)/* If * P1 <P2, note that * P1 and * P2 are actually a and B */
{
Temp = * P1;/* use the variable temp for temporary storage * P1 and a value */
* P1 = * P2;/* replace * P1 value (that is, the value of a) with * P2 value (that is, the value of B), which is equivalent to a = B */
* P2 = temp;/* Equivalent the * P2 value, that is, the temp value, to B = temp */
}
}
/* Note: the difference between this question and the above question is that it directly changes the value of A and B to achieve the purpose of real change */