Help with passing a refernce of array

I am working on some code for a class and I would like to pass an array as a reference. I want the array values available to the function. Any help would be great!

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

double sword(double x, double y, double& itmes);
double heart(double x, double y);
double triforce(double x, double y);


int main()
{
    double items[3][2]=
    {
        { 5.0, 3.0 }, //Sword
        { -3.125, 0}, //Heart
        { 1.5, 8.1 }  //TriForce
    };



    double X, Y;
    cout<<"== Zelda (version 0.000001) ==\n";
    cout<<"What is the position of our hero, Link?\n";
    cout<<"Enter X and Y coordinates separated by a space\n";
    cin>>X>>Y;
    //cout<<"Sword is at X= "<<items[0][0]<<"Y= "<<items[0][1];


    cout<<"Sword is at (5.0, 3.0).  Distance from :"<<sword(X, Y, items);

    return 0;
}

double sword(double x, double y, double& items)
{
    double tx, ty, ptx, pty, Dist;
    tx = items[0][0]-X;
    ty = items[0][1]-Y;
    ptx = pow(tx, 2);
    pty = pow(ty, 2);
    Dist = sqrt(ptx +pty);
    return Dist;
}


Last edited on
I want the array values available to the function

arrays do that normally
Last edited on
As Yanson mentioned arrays are passed as reference because you can not return them.

So you would call the function like

double sword( double x , double y , double items[] );

You do not have to specify the size of the array the compiler will find that by itself. Though if you are using multidimensional arrays you have to specify. Say for example you have a 2x4 array you can pass it like
double items[][4] The compiler will figure out the number of rows but you must pass the columns. You can however use templates for this slight problem.

1
2
template <std::size_t row , std::size_t column>
void function( int (&array)[row][column] );
Last edited on
giblit wrote:
As Yanson mentioned arrays are passed as reference because you can not return them.

Not quite. The name of an array decays to a pointer to the first element when it is fed to a function which expects a pointer, such as the sword prototype you suppy above. What is passed to the function in that case is the address of the array in the form of a pointer (which is passed by value.)


giblit wrote:
You do not have to specify the size of the array the compiler will find that by itself.

The compiler has no way of determining the size of an array pointed to by a pointer parameter which was passed by value. It is typical in C style functions which take pointers to also specify the number of elements which are at the address contained by the pointer.


giblit wrote:
The compiler will figure out the number of rows but you must pass the columns. You can however use templates for this slight problem.

Here, you pass an array by reference in the templatized code and no information is lost.


Last edited on
I wish in C++ functions are in there own scope. I could declare the array as global and be done. However I would like to know now to use reference to do it.
//Simple Do it by this
//Pass a 2D Array in a function as follows:

#include<iostream>
using namespace std;

//Function Prototype
void PrintElements(int ArrRefer[][2]);

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

//Passing Reference of 2D Array
PrintElements(arr);

cin.ignore();
return 0;
}

//Function Definition
void PrintElements(int ArrRefer[][2])
{
for(int r=0;r<3;r++)
{
for(int c=0;c<2;c++)
{
cout << ArrRefer[r][c] << " ";
}
cout << endl;
}
}
I changed the code by removing the address of key word "&" but now I get a different error. "error: expected primary-expression before ']' token". The function proto type is now

double sword(double x, double y, double items[][2]);

the body of the function is:

double sword(double x, double y, double items[][2])
{
double tx, ty, ptx, pty, Dist;
tx = items[0][0]-x;
ty = items[0][1]-y;
ptx = pow(tx, 2);
pty = pow(ty, 2);
Dist = sqrt(ptx +pty);
return Dist;
}
Topic archived. No new replies allowed.