#include<iostream>
usingnamespace 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.