![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
how do you return an array? *solved*
i quickly pieced this together so that you can get an idea of how much i need help. :o i've got an assignment to do involving arrays and passing them to and from functions. i'm trying to pass one or more arrays into a function, which i can do, but the problem comes when i try to return an array. compiling the code below gives me the following messages:
"expected unqualified-id before '[' token " "expected `,' or `;' before '[' token " and then highlights the 4th line. am i just being a dumbass or what? #include <iostream>
using namespace std;
int[] aFunction( int IntArr[] )
{
int Temp[] = IntArr;
}
int main()
{
int array[6];
for ( int i = 0 ; i < 6 ; i++ )
{
array[i] = i;
}
}thanks for the help.
__________________
There's got to be more to life than being really, really ridiculously good looking Last edited by Easter Bunny; Apr 21st, 2005 at 9:55 AM. Reason: forgot to insert an error message |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
as far as i remember arrays are passed by reference
you generall pass a pointer to the array even though you dont have to strictly specify that.
#include <iostream>
using namespace std;
void aFunction( int IntArr[] )
{
for (int i = 0; i < 6; i++){
IntArr[i] = i;
}
}
int main()
{
int array[6];
aFunction(array);
for ( int i = 0 ; i < 6 ; i++ )
{
cout << array[i] << endl;
}
for (;;);
}as you can see i put the values into the array inside the funciton and since its a reference to the array you do not need a return value, just remmeber to manipulate the array you pass otherwise the values are not altered. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
thanks for the help berto.
![]() if it's a reference variable, then shouldn't it be: void aFunction( int & IntArr[] )
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4
![]() |
Array are automatically passed by reference, hence you don't need to use the ampersand sign.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
its only arrays that this works on, normal variables are not passed by reference i am not 100% on objects but i think they are passed by value and not reference.
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4
![]() |
You are most definitely right, Berto.
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
cool! thanks. that made my day. i can go home and start panicing about my other subjects now. thanks guys.Ü
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|