reference question

Hi,

Is this
int& getLargestElement(int *array, int length);

the same as this:
int * getLargestElement(int * &x_array, int &length)
No.
Can you be so good as to help me?

In:int& getLargestElement(int *array, int length);

It means, function getLargestElement will return int&.

&int is the type returned.

Why would you put this in your function?

Without the ampersand seems to yeild the same result.
Last edited on
When you return a reference, in this case the reason is probably so that you can modify the returned value. Here's a similar example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int& getLastElement(int *array, int length) {
    return array[length - 1];
}

int main() {
    int arr[5] = { 0, 1, 2, 3, 4 };

    // modify the last element
    int& ref = getLastElement(arr, 5);
    ref = 9;

    // print the new array
    for (int i = 0; i < 5; ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}
0 1 2 3 9

If you did this without the ampersand, you would not be able to do that – you would get a local copy of the value.

The version with pointers that you had in your OP could be used similarly, though it's uglier and appears to modify the length of the array, from looking at the signature.
Last edited on
See the two functions in:
http://www.cplusplus.com/reference/vector/vector/operator[]/

Both return a reference. For the non-const version that is a must.
One could return a value instead of const reference, but that implies a copy.

1
2
3
int   foo(); // returns a value
int & bar(); // returns a value as a reference
int * gaz(); // returns a pointer 

Both foo() and bar() do return an integer. The gaz() returns a pointer.


Without the ampersand seems to yeild the same result.

Almost, but not quite ...
1
2
3
4
5
6
7
8
int& getElement(int *array, int pos ) { return array[pos]; }

int foo = getElement( arr, 42 );
foo = 7; // does not change arr
int & bar = getElement( arr, 42 );
bar = 7; // does change arr, same as: arr[42] = 7;

getElement( arr, 42 ) = 7; // changes arr 

The foo is a copy of the value that is hold in *(arr+42).
The bar is a reference to value in location arr+42.
Last edited on
I was experimenting with return by reference and came across a behavior that needed some explaining:
In this line (3 below) if I l eave out the ampersand the array element isn't changed. If I leave the ampersand in, the array element is changed - just as it has been explained to me in this post. In order for lastElement to be equal to array, don't the types have to be the same?

int & lastElement = array[length-1];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
 
int& getLastElement(int * array, int length)
{
    int & lastElement = array[length-1];
    std::cout<<"Last element: "<<lastElement<<std::endl;
    return lastElement;
}
void printArray(int * array, int length)
{
    for (int i = 0; i<length; ++i)
    {
        std::cout<<array[i]<<"  ";
    }
}
 
int main()
{
    int array[5] = {1,45,2,9359,12};
    int length = 5;
    printArray(array, length);
    std::cout<<std::endl;
    int& ref = getLastElement(array,length);
    ref = 67;
    printArray(array, length);
    return 0;
}
A reference is not a variable and not quite part of the type in the way you seem to think.
1
2
int foo;
int & bar = foo;

The foo is a variable and has type int.
The bar is an alias for foo. The foo has type int.

On your line 5 you do create an alias for array[length-1]. The type of array[length-1] is int.
You could have written the function like this too:
1
2
3
4
5
int& getLastElement(int * array, int length)
{
    std::cout<<"Last element: "<<array[length-1]<<std::endl;
    return array[length-1];
}


When you call the function:
1
2
int & ref = getLastElement(array,length);
int   val = getLastElement(array,length);

Is effectively same as:
int & ref = array[length-1]; // alias
int val = array[length-1]; // copy[/code]

If you do remove the reference from the "function's return type", then the returned value is a copy:
int getLastElement(int * array, int length);
Almost like writing:
1
2
3
int tmp = array[length-1];
int & ref = tmp;
int   val = tmp;

Almost, because there is no named variable 'tmp'; the return value of a function is an unnamed temporary variable that ceases to exist when the statement is over. One cannot create a non-const reference/alias to unnamed temporary.
Thank you
Topic archived. No new replies allowed.