returning arrays c++

Discuss all aspects of programming here.

Moderator: The Mod Squad

returning arrays c++

Postby bdiec482 » Thu May 12, 2005 4:00 am

Hey all,

Believe it or not, i teach computer science at a high school in northern wisconsin. Which means i have had two semesters of c++ in college. I can't remember how to return an array from a function. One of my students is on me to figure this out. This class i am teaching is not learning about pointers, but i suspect pointers is the way to go. Could someone write a two line function with the syntax on how to return an array? Thanks so much, and don't worry...i won't give myself credit for the discovery. haha

Code hard,


Postby Roach412 » Thu May 12, 2005 5:44 am


Returning an array from a function: (how to return an array from a function)

Something really annoying about C++ is that you can't return an array from a function (method) like one does in Perl or Java. You have to create the array before calling the function, then call the function, passing a pointer to the array. Within the function, the array is filled.


Code: Select all
void CRomeView::test (int* x)
 x[0] = 21;   
 x[1] = 10;

void CRomeView::test2 (CString* x)
 x[0] = "text1";
 x[1] = "text2";

Calling the function:

int myint[2];
AfxMessageBox( myint[1] );

CString cs[2];
AfxMessageBox( cs[1] );

After calling the function, we have the 'return' values in the array we created before.


Lian Li Lancool First Knight Series PC-K59W
Intel Core i7-960 Bloomfield 3.2GHz
EVGA X58 FTW3 132-GT-E768-TR
G.SKILL Ripjaws Series 12GB (3 x 4GB)
Crucial M4 128gb SATAIII SSD x2
Crucial M4 256gb SATAIII SSD
OCZ Saber 1000 960GB Enterprise SSD
Corsair Professional Series HX850 PSU
Dell UltraSharp U2713HM 27" w/drop ceiling mount
Logitech G930 7.1 Headset
Logitech G402 Hyperion Fury
Black Belt 2nd Degree
Black Belt 2nd Degree
Posts: 2714
Joined: Mon Aug 09, 2004 7:33 pm
Location: Milwaukee - Wisconsin

Postby pa3lsvt » Thu May 12, 2005 6:20 pm

That being said, if you truly want to "return" an array from a function, you have to use pointers as you mentioned. Technically, it's just passing addresses as pointers and then treating them as if they were an array, which is really just a pointer and an offset = index * sizeof(var-type).

Something along these lines, except I seem to think that this should involve '&' (addressof operator) somewhere for some reason:

Code: Select all

int* mysub(int init)
     int myarray[3];
     for (int i = 0; i < 3; i++)
          myarray[i] = init++;
     return myarray; //might be: return myarray&;

int main()
     int* values = mysub(int(10));
     for (int i = 0; i < 3; i++)
          cout << "myarray[" << i << "] = " << values[i] << endl;


myarray[0] = 10
myarray[1] = 11
myarray[2] = 12

Returning Pointers

Postby BorderGuard » Sat Jul 16, 2005 8:25 pm

Being at about the same level as you, with 3 courses in C++ I can see both of the aforementioned methods but you have to be a little farther along to see Roach 412s method. Pointers and address of operators have given me problems in the past and I have been taught both ways. Both ways look good to me but I did not even see the 412s method until my 2nd semester so therefore I would favor pa3lsvts.

I hope I am not too confusing but from someone who has had the same problems in the past that is my recommendation.

Postby SkReaP » Mon Jul 18, 2005 8:51 am

I might be oversimplifying, but you can just do this (if I understand the question)

void main(void)
int Array[10];

void func1(int &Array)

Or something like that. Haven't used C++ in a while. (kinda sad - I have a degree in computer science!)
Brown Belt
Brown Belt
Posts: 266
Joined: Wed Feb 11, 2004 3:01 pm
Location: Anyplace with internet

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest