Home > Back-end >  C language pointer problem, operation success but no results
C language pointer problem, operation success but no results

Time:10-07

Program, use a custom function to the main function of the array and the minimum maximum, minimum exchange with the first element, the maximum and tail element exchange, the maximum and minimum value stored in the main function of two variables respectively, # include & lt; Stdio. H>
Void exchange (int * q, int n);
Int main ()
{
,7,4,6,9 int a [5]={2};
int i;
Exchange (a, 5);
for(i=0; i<5; I++)
{printf (" % 3 d ", a [I]);
}
return 0;
}
Void exchange (int * q, int n)
{
Max=q, int * * min=q, * k=q, temp.
for(; K{{if (* k> * Max) {Max=k; }; } {the if (* k<* min) {min=k; }; }
}
Temp=* q;
* q=* min;
* min=temp;
Temp=* (q + n - 1);
+ n - 1) * (q=* Max.
* Max=temp;
}

CodePudding user response:

What's the problem for bosses to see a pointer

CodePudding user response:

Compare your logic chaos, himself,

PArray void exchange (int * and an int nLen, int * pMax, int * pMix)
{
If (pArray==NULL | | nLen & lt;=0) return;

Int nMax, nMin;
NMax=nMin=0;

for(int i=1; i{
If (pArray [I] & gt; NMax pArray [nMax])=I;
Else if (pArray [I] }

If (pMax! * pMax==NULL) pArray [nMax];
If (pMix! * pMix==NULL) pArray (nMin);


If (nMin!=0)
{
PArray [0]=pArray [0] + pArray (nMin);
PArray [nMin]=pArray [0] - pArray (nMin);
PArray [0]=pArray [0] - pArray (nMin);
}

If (nMax!=nLen - 1)
{
PArray] [nLen - 1=pArray [] nLen - 1 + pArray [nMax];
PArray [nMax]=pArray [] nLen - 1 - pArray [nMax];
PArray [nLen - 1]=pArray [] nLen - 1 - pArray [nMax];
}
}

CodePudding user response:

Void exchange (int * q, int n, nMin int * and an int * nMax ).//need two parameters to return to the main function of minimum and maximum
Int main ()
{
int nMax, int nMin;
,7,4,6,9 int a [5]={2};
int i;
exchange (a, 5, & amp; NMin, & amp; NMax);
the for (I=0; i<5; I++)
{printf (" % 3 d ", a [I]);
}
return 0;
}
Void exchange (int * q, int n, int * nMin, int * nMax)
{
Int k=1;
Int iMax=0, iMin=0;//said minimum and maximum values in the array subscript, first initialized array

If (n<=0) return;
If (n==1) {* nMax nMin==* * p; return; }

Where//find the minimum and maximum values
for(; KIf (p [k]

Else if (p [k] & gt; P [iMax] iMax=k;
}

//the exchange of minimum
* nMin=p [iMin];
P=p [iMin] [0];
P [0]=* nMin;
//exchange's largest
* nMax=p (iMax);
P=p [iMax] [n - 1);
P/n - 1]=* nMax;
}

  • Related