How do i return an array?

Hello, I am a new user, and i have run into a problem.
This function is not returning the array, which seems to be a common problem. I have tried implementing solutions found online, and i cannot find anything.

All that is included is the function, my includes, and my namespace.
Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int initialize()
{
    srand ( time(NULL) );
    int cube [26][26][26];
    int x,y,z;
    for(x=0;x<26;x++)
    {
          cout << "Floor " << x <<  " Initialized \n";
          for(y=0;y<26;y++)
          {
                for(z=0;z<26;z++)
                {
                      cube [x][y][z] = (rand() % 9 + 1);
                }
          }
    }
    return cube;
}



On a side note, at least for this function, I am not looking for efficiency, just functionality.
Last edited on
int initialize()

the return type is specifically an integer, so that won't work. Try using this instead.

int*** initialize()

That should work if I remember correctly.
Last edited on
Your function is fundamentally flawed.

You cannot return an array (by which I mean, you can't return some object that is all the data); the best you can manage is to return a pointer . When you make an array, like this:

int someArray[26];

if you then pass someArray to something, you are effectively passing a pointer to an int (I know you've got a multi-dimensional array, so you're passing a pointer to a pointer to a pointer to an int; I've simplified this example).

Now, passing a pointer is by no means bad. However, you have made your array inside the function, in local scope. When the function ends, everything in local scope will be destroyed. All that memory that was the array (the 26 * 26 * 26 int values) will be tidied up. Gone. You return a pointer to something that doesn't exist anymore. This is bad. It's very, very bad.

Your options are:

1) Create the array inside the function using new, and then remember to delete it in the future when you don't need it. This is a pain, particularly when you're making multi-dimensional arrays.

2) Create the array outside the function, and pass the array (i.e. a pointer) into the function, so that the function can fiddle with the array and then when it's finished, the array will still exist.

3) Use a proper grown-up C++ container. I suggest vector.


As an aside, I see you include cstdlib and stdlib.h - this is pointless. You're coding in C++, so just include cstdlib. See also cstring (or did you actually mean proper C++ string? It's very, very different to string.h and cstring), cstdio, ctime.
Last edited on
OK, I'll try vectors, but first ill use proper grown-up respect for other people. Thanks.
you have made your array inside the function, in local scope.


I completely missed that, didn't notice it at first glance.
OK, I'll try vectors, but first ill use proper grown-up respect for other people. Thanks


Your patronising attitude is as unwelcome as it is unwarranted. Nonetheless, I take no offense. It is C++ vectors that are "grown-up"; this implies nothing about you. Do I think of C programmers as children? Of course I don't.
The following is a simple way of doing it if you don't want to delve into vectors yet:

function:
1
2
3
4
5
6
void myFunction(int *myArray[])
{
    for (int i = sizeof(*myArray)/sizeof(int) ; i > 0 ; i--)  //Find the number of elements in an array
        *myArray[i] = 0;                                   //Do what you want with each element
    return;
}


Call:
1
2
int OutsideArray[10];            // Define your array of a size
myFunction(&OutsideArray); // Pass the address of your array into the function 


Edit: I've found out that the sizeof is rather compiler dependant. I've seen a few solutions where people will sometimes pass the size into the function as a seperate value instead of trying to calculate it in the function itself.
Last edited on
sizeof(*myArray)/sizeof(int)

Does that work inside a function? I remember I tried passing in arrays into functions before, but when I do the sizeof(array)/sizeof(array[0]) the sizeof(array) only gave me back the size of an int pointer. Since then I just normally passed the size into the function as a parameter.

Never really tried it on a pointer to an array though so I was just a bit curious if that does work.
No, it does not work. Stewbond's code is incorrect and a little confusing.
Topic archived. No new replies allowed.