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.
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 deleteit 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.
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.
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.