# A question on passing arrays by reference - thinking loud!

#### Quasar Chunawala

Hi all,

I have written a C++ program that passes an array of 10 EuropeanOptions, by reference to a function intrinsicValues(). The call and the function definition is as below.

The first argument to instrinsicValues(), EuropeanOption (&opt) [10] can be understood as, reference to EuropeanOption[10].

Now, references in C++ are like constant pointers that are automatically dereferenced. So, why can't the compiler be happy with EuropeanOption& myOpt? Ultimately, all I need is a handle to myOpt in memory to access the array. Or that's just standard C++ syntax?

C++:
class EuropeanOption
{
public:
double sigma, r, S, T, K;
char type;

EuropeanOption()
{
S = 100; T = 1; r = 0.10; sigma = 0.05; K = 100; type = 'C';
}
};

C++:
//We can create an array of instances
EuropeanOption myPortfolio[10];

for (int j = 0; j < 10; j++)
{
myPortfolio[j].K = 105 - j;
myPortfolio[j].print();
}

intrinsicValues(myPortfolio,10);

C++:
void intrinsicValues(EuropeanOption (&opts) [10], int size)
{
double value = 0;
for (int i = 0; i < size; i++)
{
if (opts[i].type == 'C')
{
value = (opts[i].S >= opts[i].K ? opts[i].S - opts[i].K : 0);
std::cout << "Intrinsic value = " << value << std::endl;
}

if (opts[i].type == 'P')
{
value = (opts[i].S <= opts[i].K ? opts[i].K - opts[i].S : 0);
std::cout << "Intrinsic value = " << value << std::endl;
}
}
}

#### mhy

I think it is just syntax. It is clarifying to the compiler that you want to pass a reference to an array, rather than an array of references

i.e.
C++:
void intrinsicValues(EuropeanOption &opts[10])
This is illegal as it is an array of references.

C++:
void intrinsicValues(EuropeanOption (&opts)[10])
This is clear - it is a reference to a size 10 array of EuropeanOptions.

EuropeanOption &myOpt to me looks like "reference to a single EuropeanOption" and I wouldn't expect code to compile if I pass arrays of EuropeanOption into it.

Last edited:

#### mhy

By the way, you probably know this but you can just take EuropeanOption [] (or EuropeanOption *) to pass an array of any length and this is done automatically via pointer so you don't need to worry about copying overhead.

Or use std::vector &

#### Quasar Chunawala

By the way, you probably know this but you can just take EuropeanOption [] (or EuropeanOption *) to pass an array of any length and this is done automatically via pointer so you don't need to worry about copying overhead.

Or use std::vector &

Yeah, I find the pointer/array syntax more friendly.

I am yet to learn vectors. Exciting time. Also, I learnt that C++ has very strict type checking and how the C++ compiler does constant folding - that is it evaluates constant expressions at compile time and does not allocate storage to constants.

Further, I now know about anonymous(temporary) objects. At one place, I had written

C++:
Complex z = z1 * z2

But, I missed writing const in the function arguments to operator =().

C++:
Complex& Complex::operator =(Complex& z)

should really be

C++:
Complex& Complex::operator =(const Complex& z)

While I read in theory, that temporary objects such as (z1 * z2) are const, I learnt it the hard way! The best way to learn is to practice and practice and practice!

#### Pavlos Sakoglou

Yeah, I find the pointer/array syntax more friendly.

I am yet to learn vectors. Exciting time. Also, I learnt that C++ has very strict type checking and how the C++ compiler does constant folding - that is it evaluates constant expressions at compile time and does not allocate storage to constants.

Further, I now know about anonymous(temporary) objects. At one place, I had written

C++:
Complex z = z1 * z2

But, I missed writing const in the function arguments to operator =().

C++:
Complex& Complex::operator =(Complex& z)

should really be

C++:
Complex& Complex::operator =(const Complex& z)

While I read in theory, that temporary objects such as (z1 * z2) are const, I learnt it the hard way! The best way to learn is to practice and practice and practice!
I just want to point out that Complex c = c1*c2; calls the copy constructor, not the assignment operator.

#### Quasar Chunawala

I just want to point out that Complex c = c1*c2; calls the copy constructor, not the assignment operator.

Gee yeah!! I read it up online. Thanks!!

C++:
Complex c1(3.0,4.0);
Complex c2(5.0,12.0);

c2 = c1; //Akin to c2.operator=(c1)
Complex c3 = c1; //Akin to c3(c1) - calls copy constructor

Replies
5
Views
962
Replies
3
Views
2K
Replies
7
Views
3K
Replies
2
Views
2K
Replies
0
Views
2K