passing array from function back to main function

hi
i am wondering that is it possible to pass an array from a function back to the main function
i wrote a code like this

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
#include<iostream>
using namespace std;

int ary(int& ary1[]);

int main()
{
    int ary1[5];
    int i;

    cout << "input 5 integers:" << endl;

    for (i=0; i<5; i++)
        cin >> ary1[i];

    cout << endl;
    cout << "the integers you've inputed is ";

    for (i=0; i<5; i++)
        cout << ary1[i];

    cout << endl;
    cout << "-------------------------------------------------" << endl;

    ary(aryl[]);

    cout << endl;
    cout << "the new integer is ";

    for (i=0; i<5; i++)
        cout << aryl[i];
}

int ary(int& ary1[])
{
    cout << endl;
    cout << "enter new 5 integers:" << endl;

    for (int i=0; i<5; i++)
        cin >> aryl[];
}


but when i compile, the compiler told me that
1
2
3
4
5
6
line 4: error: declaration of 'ary1' as array of reference
line 25: error: 'aryl' was not declared in this scope
line 25: error: expected primary-expression before ']' token
line 35: error: declaration of 'ary1' as array of reference
line 40: error: 'ary1' was not declared in this scope
line 40: error: expected primary-expression before ']' token


is there anyway to pass the array back to the main function?
When you just use the name of the array to pass it as a parameter, it is assumed to be a pointer to the first element. Therefore, you will be modifying the actual array; no need for references. The problem is the when you use the & like that you are basically declaring an array of "references to ints" which you can't do.
when you call het ary-function, you have te write this: ary(arty1).
the function takes a pointer te the first element, thats why. if you write this: ary(ary1[]) then you pass the content of ary1 on index 0.
"was not declared in this scope" usually means you did not declare something OR you made a typo. You made some typos in this case.


To call a function with a reference or pointer argument do this : func(var);

To define a function with a referenced argument do this: void func(int &var);

The array brackets get translated to a pointer so void func(int var[]); means void func(int *var);

Passing by reference is usually the best choice. Passing by const reference will protect your data from changes (void func(const int &var);)
you have some error in code:

line 8: int ary1[5];

line 25: ary(aryl[]);

line 35: cout << aryl[i];

no variable declaration for aryl, I think you need use ary1 variable

same as in line 40, see on line 34:

line 34: int ary(int& ary1[])

line 40: cin >> aryl[];

line 25: ary(aryl[]);

usage: ary(aryl); to passing an array to a function



thanks guys now i got it

i knew where is my mistakes already
its about the reference symbol '&'
it shouldn't be used to pass an array to another function

thanks again for the advice, ReeV, turbozedd, hannes, and Zhuge.
Topic archived. No new replies allowed.