Click to See Complete Forum and Search --> : return reference
arlenagha
03-05-2003, 03:10 AM
I know how item_array[2] becomes 0 but i don't know why. since array[] is not a reference to item_array[2] how does the function biggest, which returns a reference to array[bigges], assign 0 to item_array[2]???
could someone please explain this to me, thank you.
#include <iostream>
int& biggest(int array[], int n_elements){
int index;
int biggest;
biggest = 0;
for(index = 1; index < n_elements; ++index){
if(array[biggest] < array[index])
biggest = index;
}
return(array[biggest]);
}
int main(){
using namespace std;
int item_array[5] = {1, 2, 5000, 3, 4};
cout << "The biggest element is " << biggest(item_array, 5) << endl;
biggest(item_array, 5) = 0;
cout << item_array[2] << endl;
return 0;
}
truls
03-05-2003, 04:14 AM
biggest(item_array, 5) = 0;
Here you call the function biggest.
Biggest then returns a reference to array element 2.
You then assign 0 to this element.
Biggest doesn't change anything, it only returns a reference to the array element with the largest value. If you assign any value to the return value from biggest you change that array element.
arlenagha
03-05-2003, 04:31 AM
but how does item_array[2] become 0???
truls
03-05-2003, 10:43 AM
Ok. You have an array item_array[5]. Which then looks like this:
item_array[0] = 1
item_array[1] = 2
item_array[2] = 5000
item_array[3] = 3
item_array[4] = 4
You then call biggest, which returns a reference to the biggest element, which obviously is item_array[2]. A reference is like a pointer, meaning that if you change the reference you also change the location which it references.
In other words biggest returns an int reference which looks like:
int& ---> item_array[2].
When you change the return value of biggest, you ALSO change item_array[2], since the return value from biggest references item_array[2].
So item_array becomes 0 in the line:
biggest(item_array, 5) = 0;
This line is like writing:
int& ref = biggest(item_array,5); // ref now references item_array[2]
ref = 0; // changes the value ref references, which is item_array[2]
T.
arlenagha
03-05-2003, 04:10 PM
ok. function biggest returns an int reference to array[], not item_array[]. isn't that right?? then how is item_array[] connected to array[] which causes it to become 0???
i am new to c++ so that is why i am having these difficulties.
truls
03-06-2003, 04:21 AM
Arrays in C/C++ are passed as pointers, so when you write biggest( int array[], ... ) this is the same as writing biggest( int* array, ... ). So basically array is simply a pointer to item_array. So when you change array in biggest, you also change item_array. And when you return a reference to array, this is really a reference to item_array.
Here is a program that might explain this:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void test( int array[] )
{
cout << "The address of array is: " << std::hex
<< (int)array << endl;;
array[0] = 1000;
}
int main()
{
int test_array[3] = { 0,1,2 };
cout << "Value 0 is: " << test_array[0] << endl;
cout << "The address of test_array is: " << std::hex
<< (int)test_array << endl;
test( test_array );
cout << "Value 0 is now:" << test_array[0] << endl;
return 0;
}
arlenagha
03-06-2003, 07:32 AM
thank you truls, that helped alot. :)