Reputation: 153
New to pointers and reference so I'm not sure on much about this but I'm trying to pass the pointers *minDataValue and *maxDataValue so that their values are changed when they come back from the functions. As of now in the code, their values don't change (as evident by the testing code), how to I set them up and what do I have to change to make them pass by reference so that the value can change when the function is done. Thanks!
void findMinAndMax(int array[], int size, int *min, int *max) {
int smallest = array[0];
int largest = array[0];
min = &smallest;
max = &largest;
for (int i = 1; i < size; i++)
{
if (array[i] > largest){
largest = array[i];
}
if (array[i] < smallest){
smallest = array[i];
}
}
// testing code
cout << *min << endl;
cout << *max << endl;
}
int *makeFrequency (int data[], int dSize, int *minDataValue, int *maxDataValue) {
cout << *minDataValue << endl;// testing code
cout << *maxDataValue << endl;// testing code
findMinAndMax(data, dSize, minDataValue, maxDataValue); // How do I pass this so that the value changes after the min and max are found?
cout << *minDataValue << endl; // testing code
cout << *maxDataValue << endl;// testing code
}
int main() {
int dSize;
int *ArrayOfInts;
cout << "How many data values? ";
cin >> dSize;
ArrayOfInts = new int [dSize];
getData(dSize, ArrayOfInts);
int *frequency, min, max;
frequency = makeFrequency(ArrayOfInts, dSize, &min, &max);
}
Upvotes: 1
Views: 471
Reputation: 3111
There are two ways of achieving it. I would give a small example involving a function & a pointer.
void f1 (int** p) // argument is pointer to pointer
{
}
void f2 (int*& p) // argument is reference to pointer
{
}
int main()
{
int x = 5;
int* p = &x;
f1 (&p); // pass address of pointer
f2 (p); // pass reference (C++ type) of pointer
return 0;
}
f1
is more of C
than C++
unlike f2
. I like f2
however because I feel it is type safe
& more convincing as a reference
. Moreover pointers
are wild & shouldn't be messed with much, Same goes for pointers to pointers
!!!!
Upvotes: 0
Reputation: 324
You have to use pointer of pointer or pass by reference (@Sami Sallinen explained) to change the value *minDataValue and *maxDataValue inside the functions
//Using pointer of pointer
void findMinAndMax(int array[], int size, int **min, int **max)
{
int *smallest = &array[0];
int *largest = &array[0];
min = &smallest;
max = &largest;
for (int i = 1; i < size; i++)
{
if (array[i] > *largest){
*largest = array[i];
}
if (array[i] < *smallest){
*smallest = array[i];
}
}
// testing code
cout << **min << endl;
cout << **max << endl;
}
int *makeFrequency (int data[], int dSize, int *minDataValue, int *maxDataValue)
{
findMinAndMax(data, dSize, &minDataValue, &maxDataValue);
}
Upvotes: 1
Reputation: 3496
You can either take the function take a pointer-to-pointer:
int *makeFrequency (int data[], int dSize, int **minDataValue, int **maxDataValue)
and then modify
*minDataValue and *maxDataValue
inside the function. In this case, the calling code must passa a pointer to pointer explicitly:
int *min, *max;
makeFrequency (data, size, &min, &max)
The second possiblity is to pass a reference to a pointer:
int *makeFrequency (int data[], int dSize, int*& minDataValue, int *& maxDataValue)
In which case you just modify the pointer inside the function and pass a pointer to the function.
I'd prefer the 1st way, because then the calling code also looks different, so the fact that the pointers may get modified inside the function is visible from the calling code too.
Upvotes: 0