-1

I want to pass value of array in function by reference

  1. My first try with variable (it works well):

    #include <iostream>
    using namespace std;
    
    int input(int &);
    int main()
    {
        int a;
        input(a);
        cout << "the value : " << a << endl;
        system("pause");
        return 0;
    }
    
    int input(int &a)
    {
        cin >> a;   
    }
    
  2. But when i change a to a[] like in the following code, it doesn't work

    #include <iostream.h>
    using namespace std;
    
    int input(int &[]);
    int main()
    {
        int a[2], i;
        input(a);
        for(i=0;i<=2;i++)
        {
            cout << a << endl;
        }
    
        system("pause");
        return 0;
    }
    
    int input(int &a[])
    {
        int i;
        for(i=0;i<=2;i++)
        {
            cin >> a[i];
        }  
    }
    
sjaustirni
  • 3,056
  • 7
  • 32
  • 50
user3527076
  • 59
  • 2
  • 6
  • At least the array parameter in the function declaration must match the exact length of what's passed to it: `int input(int &a[2])`, you are trying to pass two parameters, where only one was declared: `input(a, 2);` – πάντα ῥεῖ May 10 '14 at 13:48
  • You have to call your method as `input(a)` in the second case – Ashalynd May 10 '14 at 13:49
  • 2
    You should say what "not working" means - did you get a compiler error? runtime error? Crash? Looks to me like you should get a compiler error. – Bryan May 10 '14 at 13:50
  • You should strongly consider using `std::vector` instead. Much easier to use and no need to worry about size. – crashmstr May 10 '14 at 13:53
  • yes arrays of references are illegal – user3527076 May 10 '14 at 13:53

3 Answers3

2

This record

int &[]

denotes an array of references that is forbided in C++.

Declare the function the following way

void input( int ( & )[2] );

Also you are using invalid range of indeces. The valid range is [0, 1]. So the function will look as

void input( int ( &a )[2] );
{
   for ( size_t i = 0; i < 2; i++ ) std::cin >> a[i]; 
}

Also you could define the function the following way

void input( int a[], size_t n );
{
   for ( size_t i = 0; i < n; i++ ) std::cin >> a[i]; 
}

and call it as

input( a, 2 );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

It should be:

void input(int (&a)[2]);

or simply

void input(int (&)[2]);

But in function definition, you need also to give the array name:

void input(int (&a)[2]) {...}

You need to use (&array) to clarify to the compiler that you want a reference to an array, rather than the (invalid) array of references int &array[2];.

And call it like this:

input(a);
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
1

The syntax for returning and taking array references is:

template<typename T, size_t N>
T(&do_nothing(T(&arg)[N]))[N]
{
    return arg;
}

Or, more simply with a templated type alias:

template<typename T, size_t N> using arrayref = T(&)[N];

template<typename T, size_t N>
arrayref<T, N> do_nothing(arrayref<T, N> arg)
{
    return arg;
}

When you have defined the previous type alias template, it becomes very simple to work with array references, e.g. This is how you would declare a function that takes a reference to an array of two integers:

void give_me_two_ints(arrayref<int, 2> two_ints);

To be used like:

int a[] = {1,2,3};
for(auto const& i : do_nothing(a)) std::cout << i << "\n";
danielschemmel
  • 10,885
  • 1
  • 36
  • 58