Home > Back-end >  Why the data generated is not the elements in the array?
Why the data generated is not the elements in the array?

Time:03-24

# include & lt; Iostream>
using namespace std;

Void f (int * a, int n, int * Max, int * min)
{
Max=min=a;//here if change to * Max min=*=a, [0]. Is the right
for(int i=1; I{
If (* maxIf (* min> A [I]) * min=a, [I].
}
}

Int main ()
{
,5,3,9,0,8,1,7,6,4 int a [10]={2};
Int the Max and min.
F (a, 10, & amp; Max, & amp; Min);
Cout<& lt;" Max: "& lt; Cout<& lt;" Min: "& lt;
return 0;
}

CodePudding user response:

The Max and min is a pointer, in f you modify his point, the function, is restorable, it's no use

CodePudding user response:

reference 1/f, the truth is right or wrong response:
Max, min is a pointer, in f, you modify his point, the function, is restorable, useless

But the pointer can be accessed across functions and data output, Max=min=isn't a said Max and min at the same time, point to an array of a first address?

CodePudding user response:

reference kwwking reply: 3/f
Quote: reference 1/f, the truth is right or wrong response:
Max, min is a pointer, in f, you modify his point, the function, is restorable, useless

But the pointer can be accessed across functions and data output, Max=min=isn't a said Max and min at the same time, point to an array of a first address?

You need to know c/c + + function call in the basic knowledge of the parameters,
You pass a parameter to a function, it is a copy of it, such as the code you & amp; Max, & amp; Min is passed to the fn, then have a Max address in fn copy of if the address to write can pass the results back to the memory unit, and you changed the address, because he is a copy of function after the natural and the

CodePudding user response:

To this:
 # include & lt; Iostream> 
using namespace std;

Void f (int * a, int n, int & amp; Max, int & amp; Min)
{
Max=min=* a;
for(int i=1; I{
If (max
If (min> A [I]) min=a, [I].
}
}

Int main ()
{
,5,3,9,0,8,1,7,6,4 int a [10]={2};
Int the Max and min.
F (a, 10, Max, min);
Cout<& lt;" Max: "& lt; Cout<& lt;" Min: "& lt;
return 0;
}

CodePudding user response:

reference 5 floor QZJHJXJ reply:
change to this:
 # include & lt; Iostream> 
using namespace std;

Void f (int * a, int n, int & amp; Max, int & amp; Min)
{
Max=min=* a;
for(int i=1; I{
If (max
If (min> A [I]) min=a, [I].
}
}

Int main ()
{
,5,3,9,0,8,1,7,6,4 int a [10]={2};
Int the Max and min.
F (a, 10, Max, min);
Cout<& lt;" Max: "& lt; Cout<& lt;" Min: "& lt;
return 0;
}
hello great god, but you are here with the reference, in fact is the Max and min assignment for an array of data, if you try to make the Max and min as a pointer, whether it is feasible to let them to first array address? thank you

CodePudding user response:

Novice basically don't understand the pointer in the function parameter problem
This is actually from the compilation principle and the principle of the program is running,
That is to say when a piece of code function call, can understand to jump from one space to another space to execute code, which requires passing parameters, and the parameters of the code inside the function to use, it is a , local variables in the process of parameter passing by assignment, so you are in operation parameter in a function, external code does not directly affect the function of data, and a pointer, before using * solutions for reference, just a int said address data, so in the process of parameter passing, just put the address values assigned to the function of internal parameter variable, you inside the function through the assignment to change the value of the local variable, and can't change the outside of the Max and min values,
As for upstairs QZJHJXJ answers, he on function parameters, using int & amp; (int type of reference, this is a c + + feature, skip the pointer is difficult to understand, but also has its own constraints, nature is the object reference operation is equal to the object itself,

CodePudding user response:

Pointer * * Max min is a local variable within the function f (), it is in memory of a piece of space, storage of data is int Max the address of the variable, assume that the address is 0001 now, and then in f () function to than size, find the largest number of address values assume is 1005, we will change the * stored in the Max value to 1005, function performs end back to the main function, the output cout<& lt;" Max: "& lt; # include & lt; Iostream>
using namespace std;

Void f (int & amp; A, int n, int * * Max, min) int * *
{
* Max min=*=& amp; a;
for(int i=1; I{
If (* * max<* (& amp; A + I)) * Max=(& amp; A + I);
If (* * min> * (& amp; A + I)) * min=(& amp; A + I);
}
}

Int main ()
{
,5,3,9,0,8,1,7,6,4 int a [10]={2};
Max, int * * min;//here two pointer variable, and a pointer to the function of the same name, but not in the same space
F (* a, 10, & amp; Max, & amp; Min);
Cout<& lt;" Max: "& lt; <* maxCout<& lt;" Min: "& lt; <* min
return 0;
}
  • Related