For the many people put forward C + + in the pointer difficult problem to make a summary:
Pointer learning is not the key to the concept is not clear, the simple point is that the book does not seriously look, pointer learning is like a person in learning to spare the password not to see more than learn more practice is not, the following are two very classic examples, many books have, the focus of learning is to understand *x and x understanding, they are not the same, * What x represents is actually the variable a itself, and X represents the address of variable A in memory, and if you want to understand, you can output the observation cout<<*x "|" x;, when the int *x is defined; x=&a understanding of the problem. Carefully read and contact the following two examples I think the pointer problem is not difficult!
#include <stdio.h>
Main ()
{
int a,b;/* Define A,B Two shaping variables for entering two integers */
int *point_1,*point_2,* Temp_point; /* Define three pointer variable */
scanf ("%d,%d", &a,&b);/* Formatted input A,B value */
point_1=&a;/* The value of the pointer variable point_1 to the address of variable A */
point_2=&b; /* The value of the pointer variable point_2 to the address of variable B/
if (a<b)
{
temp_point=point_1;/* The temp_point here is for temporary storage of the point_1 value, which is the address of variable a. * /
Point_1=point_2/* Assigns point_2 value to Point_1 */
Point_2=temp_point;
/* Because the value of point_1 has been changed can not be found, the use of temporary storage in front of the Temp_point back to the original point_1 value given to Point_2, hit the point_1 and point_2 value of the purpose of the swap */
}
printf ("% d,%d ", *point_1,*point_2); /* Use *point_1 and *point_2 to distinguish the values that point to B and a to show self-love on the screen */
}
/* This problem needs to be noted and understood yes this method does not change the value of the variable a,b simply by using pointer variables to store the addresses of a and B respectively, And then swap the values of the two pointer variables. In fact, is stored in the
pointer variable A and B address exchange, in the use of *point_1 and *point_2 to show the replacement value of the *point_1 is actually a, this algorithm does not really change the value of A,b, Instead,
use pointers for address exchange to sort by size.
*/
#include <stdio.h>
Main ()
{
int a,b;/* Define A,B Two shaping variables for entering two integers */
int *point_1,*point_2 ; /* Define three pointer variable */
scanf ("%d,%d", &amP;A,&B); /* Format Input a,b value */
Point_1 = &a;/* The value of the pointer variable point_1 to the address of variable A */
Point_2 = &b;/* to point the value of the pointer variable point_2 to the address of variable B */
Co Mpositor (point_1,point_2); /* Call custom sort number to pass a,b address to point_1 and point_2 */
printf ("%d,%d", a,b);/* Print out a,b value */
}
Static compositor (P1,P2)
int *P1,*P2; * Defines formal parameters P1,P2 as pointer variable */
{
int temp;/* Create temporary storage variable */
if (*P1<*P2)/* If *P1<P2, note the *P1 and *p here 2 is actually a and b */
{
temp = *P1;/* Use variable temp for temporary storage *p1 and is the value of a */
*P1 = *P2;/* Converts the value of *P1 to a value of *P2 is equivalent to the a=b */
*P2 = temp;//* The value of *P2, which is the value of temp, is equivalent to B=temp */
}
}
/* Note: This problem differs from the previous one by directly changing the value of a to B to achieve a true change.