Please write a function to sent three number choose the maximum and minimum number, and through the parameter back as a result, the part of source program is as follows:
# include & lt; Stdio. H>
Void fun (int [s], int * p1, p2 int * and an int n)
{
}
The main ()
{
Int a [3], I, Max, min,
for (i=0; i<3; I++)
The scanf (" % d ", & amp; A [I]);
Fun (a, & amp; Max, & amp; Min, 3);
Printf (" Max=% d, min=% d \ n ", Max, min);
}
I wrote the program is such, but the result of the operation is not correct, has always been a Max=0, min=51, don't know where I went wrong,
# include
Int main ()
{
Void fun (int [s], int * p1, p2 int * and an int n);
Int a [3], I, Max, min,
for(i=0; i<3; I++)
The scanf (" % d ", & amp; A [I]);
Fun (a, & amp; Max, & amp; Min, 3);
Printf (" Max=% d, min=% d \ n ", Max, min);
}
Void fun (int [s], int * p1, p2 int * and an int n)
{
int i;
P1=s, p2=s;
for(i=1; i{
If (* p1 & lt; S [I])
P1=s + I;
If (* p2 & gt; S [I])
The p2=s + I;
}
}
CodePudding user response:
P1 and p2 are local variables with the fun function stack, so change the p1=xx (local variable address changes), does not affect the function of Max, the address change p2=xx also does not affect the min address,To the following
Void fun (int [s], int * p1, p2 int * and an int n) {
int i;
* p1 p2=*=s [0];
for(i=1; i
If (* p2 & gt; S [I]) * p2=s [I];
}
}
Function does not modify the p1=xx, xx, but modify * p1=this p1 address remains the same or the address of the Max outside a function, so modify * p1 can modify the Max of memory,
CodePudding user response:
# include
Int main (int arg c, char * argv [])
{
Void fun (int [s], int * p1, p2 int * and an int n);
Int a [3], I, Max, min,
for(i=0; i<3; I++)
The scanf (" % d ", & amp; A [I]);
Fun (a, & amp; Max, & amp; Min, 3);
Printf (" Max=% d, min=% d \ n ", Max, min);
system("pause");
return 0;
}
Void fun (int [s], int * p1, p2 int * and an int n)
{
int i;
* p1 p2=*=s [0];
for(i=1; i{
If (* p1 & lt; S [I])
* p1=s [I];
If (* p2 & gt; S [I])
* the p2=s [I];
}
}
CodePudding user response:
# include & lt; Stdio. H>
Int main ()
{
Void fun (int [s], int * p1, p2 int * and an int n);
Int a [3], I, Max, min,
for (i=0; i <3; I++)
The scanf (" % d ", & amp; A [I]);
Fun (a, & amp; Max, & amp; Min, 3);
Printf (" Max=% d, min=% d \ n ", Max, min);
}
Void fun (int [s], int * p1, p2 int * and an int n)
{
int i;
* p1 p2==* * s;
For (I=1; i{
If (* p1 & lt; S [I])
* p1=* (s + I);
If (* p2 & gt; S [I])
* the p2=(s + I);
}
}