new pointers , reference i'm not sure on i'm trying pass pointers *mindatavalue , *maxdatavalue values changed when come functions. of in code, values don't change (as evident testing code), how set them , have change make them pass reference value can change when function done. thanks!
void findminandmax(int array[], int size, int *min, int *max) { int smallest = array[0]; int largest = array[0]; min = &smallest; max = &largest; (int = 1; < 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 pass value changes after min , max 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); }
you can either take function take pointer-to-pointer:
int *makefrequency (int data[], int dsize, int **mindatavalue, int **maxdatavalue)
and modify
*mindatavalue , *maxdatavalue
inside function. in case, calling code must passa pointer pointer explicitly:
int *min, *max; makefrequency (data, size, &min, &max)
the second possiblity pass reference pointer:
int *makefrequency (int data[], int dsize, int*& mindatavalue, int *& maxdatavalue)
in case modify pointer inside function , pass pointer function.
i'd prefer 1st way, because calling code looks different, fact pointers may modified inside function visible calling code too.
Comments
Post a Comment