Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 21st, 2005, 3:19 AM   #1
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
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
Easter Bunny is offline   Reply With Quote
Old Apr 21st, 2005, 3:57 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
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.
Berto is offline   Reply With Quote
Old Apr 21st, 2005, 5:14 AM   #3
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
thanks for the help berto.

if it's a reference variable, then shouldn't it be:
void aFunction( int & IntArr[] )
? ok, i tried that, but it doesn't go. odd. so you say arrays are passed by reference(wow! the reference thing makes some sense now!!), which means that the reference to the array where it's stored in memory is passed along and not actually the array itself? does this happen to all objects(arrays are objects right?)? with things like int, char,float, blah blah blah... you have to specify the reference thing then, else it just passes the value and not the variable itself!!!!! i've got it!!!! (if someone tells me i'm wrong, then i'm gonna be very confused)
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Old Apr 21st, 2005, 5:54 AM   #4
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
Array are automatically passed by reference, hence you don't need to use the ampersand sign.
BaroN NighT is offline   Reply With Quote
Old Apr 21st, 2005, 7:11 AM   #5
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
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.
Berto is offline   Reply With Quote
Old Apr 21st, 2005, 8:11 AM   #6
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
You are most definitely right, Berto.
BaroN NighT is offline   Reply With Quote
Old Apr 21st, 2005, 9:53 AM   #7
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
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
Easter Bunny is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC