### Function returning an address of (or pointer to?) an array of structures

Hello,
I'm working on a C++ program (using gedit and g++ in Ubuntu12.04) that uses a function to fill an array of structures, and I'm trying to return the array back to the caller.

From what I've learned, functions CANNOT return arrays (for some ungodly reason), so instead I'm trying to achieve my goals by having it return the ADDRESS of an array that it creates... I think. I've also tried to have it return a pointer, but I'm not sure if that's the same thing. In either case, I can't get it to do what I want.

I've poured through hundreds of forum threads all over the place where similar questions have been asked, but nobody seems to be doing exactly what I'm attempting. I REALLY hope other beginners in the future find this thread useful!

I've condensed the following code down to the barebones that will illustrate whatever it is I'm doing wrong.

 ``12345678910111213141516171819202122232425262728293031323334`` ``````using namespace std; //global constant const int MAXHOLES = 18; //global structure PAR struct PAR { int par; double avgScore; int oneThru18[MAXHOLES]; }; //function prototype PAR *func(); int main(){ PAR *arrayOfPARsPtr; //declares a pointer arrayOfPARsPtr = func();//pointer is assigned an address via func() int i; for (i; i<=MAXHOLES; i++){ // is this the array of PARs, ...? cout << *arrayOfPARsPtr[i].par << endl; } return 0; } //function definition PAR *func(){ PAR hole[MAXHOLES]; // declares an array of PAR structures //do whatever return hole; //returns an address of an array of PAR structures }``````

The code above refuses to compile.

I know there are three or four crucial places in the code that MUST have a very specific syntax to behave as I want.

1) FUNCTION PROTOTYPE
I have
 `` `` ``PAR *func();``

 `` `` ``PAR func();``

or
 `` `` ``PAR &func();``

for my purposes?

Next,
FUNCTION CALL

 ``12`` `````` PAR *arrayOfPARsPtr; arrayOfPARsPtr = func();``````

This part (above) is perhaps the most confusing of all. I have a pointer called arrayOfPARsPtr, but my problem might be that when I declare this pointer, is it interpreted as a pointer to an ARRAY of PAR structures like I need it to be, or just a single PAR structure? For the call itself,

` arrayOfPARsPtr = func();`

Should it this look like

` arrayOfPARsPtr = *func();`

instead? Or use "&" rather than a dereferenceing "*" ?

MAIN() USING THE ARRAY
 ``1234`` `````` int i; for (i; i<=MAXHOLES; i++){ cout << *arrayOfPARsPtr[i].par // is this the array of PARs, ...? }``````

I have no idea what I'm doing above, but I know I want to be able to use the array in main(), and be able to pass the array of PAR structs on to other functions later.

FUNCTION DEFINITION
I have
 ``12345`` ``````PAR *func(){ PAR hole[MAXHOLES]; // declares an array of PAR structures //do whatever return hole; }``````

I know a function can't return an array, so am I returning the array of PAR structs called "hole" correctly?
Last edited on
> The code above refuses to compile.
http://www.cplusplus.com/forum/articles/40071/#msg216270

> I'm trying to achieve my goals by having it return the ADDRESS of an array that it creates...
The problem is that your array is local to the `func()' function.
When that functions ends, the array is destroyed, so the returned value is garbage.

You can return a `std::vector' or create the array in `main()' and pass it to the function in order to be filled.
I figured it out by going it about it very differently. Before, I was trying to create a function that would RETURN an address to an array. I bet there's a way to do that, but I decided to try using a VOID function by using a pointer to my array as a parameter in that void function.

Below itsn't very neat and tidy, and I apologize for that. However, it compiles and works as I wanted it to.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````/* structureTest.cc THIS PROGRAM WORKS It first creates hole[], passes it to func by reference, which effectually returns it to main() as a processed array, hole[] */ #include using namespace std; //global constant const int MAXHOLES = 18; //global structure PAR struct PAR { int par; // 8 bits double avgScore; // 16 bits, value to be calculated int foofuu[MAXHOLES]; }; //function prototype void func(PAR *); int main(){ int i,l; PAR hole[MAXHOLES]; func(hole); //this for loop is for debugging for (i=0; i
Topic archived. No new replies allowed.