Returning an array from a function.

May 25, 2011 at 9:36pm
I am a beginner with C++ and do not know object orientated code so please go easy on me. I am trying to return a 3x3 array from a function. The aim of the function is to create the array by taking the values entered by the user then return the full array back to the main. So no parameters are initially passed in to the function itself.

Any help is appreciated.
May 25, 2011 at 9:52pm
Its impossible to return an array. Not only that but if you were to return an array that was created in the function it would go out of scope and then you would be left with junk values. You can however, create the array outside of the function and pass it as a parameter to a void function. Since arrays are really just pointers, it would automatically be passed by reference, so all you have to do is make the changes to the nine elements in the function. Your function should look roughly like this:
1
2
3
4
5
6
7
8
9
10
11

void function(int numbers[][3])
{
	for(int i = 0; i < 3; i++)
		for(int y = 0;  y < 3; y++)
		{
			// do whatever you want here
		}

	return;
}

May 25, 2011 at 10:06pm
ok thankyou :)
May 25, 2011 at 10:51pm
ascii wrote:
Its impossible to return an array.

Don't say impossible...
1
2
3
4
template <typename T>
T* makeArray(int size) {
   return new T[size];
}
May 25, 2011 at 10:56pm
That's a pointer to an array.

It's possible to actually return an array if you wrap it in a class, though:
1
2
3
4
5
6
7
8
9
10
11
template <typename T,size_t n>
struct Array{
	T array[n];
};

Array<int,10> f(){
	Array<int,10> r;
	for (int a=0;a<10;a++)
		r.array[a]=a;
	return r;
}


Disclaimer: the generated Assembly may still return a pointer, behind the scenes.
May 26, 2011 at 12:06am
Interesting idea Helios. So you're not really returning an array, but rather an instance of your Array struct?
Last edited on May 26, 2011 at 12:06am
May 26, 2011 at 12:23am
Helios, what is the difference between an array, and a pointer to an array? It is my understanding that an array is a constant pointer to the first element of the array.
May 26, 2011 at 12:28am
ascii: Define the difference at the virtual machine level.

It is my understanding that an array is a constant pointer to the first element of the array.
And you don't find any problems with this statement?
EDIT: An array is a group of adjacent objects of the same size. Not a pointer.
For T x[n]; in local scope, x is the array. x can be assigned to a pointer to obtain a pointer to x, but there's a logical gap between "x can be assigned to a pointer" and "x is a pointer".
Last edited on May 26, 2011 at 12:34am
May 26, 2011 at 12:30am
No? I'm really not that far along, not too privy at the lower level workings of c++. What should I find wrong?

Edit: I have a feeling it has something to do with delete, but not too sure.
Last edited on May 26, 2011 at 12:31am
May 26, 2011 at 12:36am
What should I find wrong?
The circular reasoning, for one.
See my edit.
May 26, 2011 at 12:54am
I define arrays as a sequence of elements of a single data type placed in sequential order in memory, so correct me if I'm wrong!

Helios, I don't really understand what you're saying about VM's there, whats your point?
Last edited on May 26, 2011 at 12:54am
May 26, 2011 at 12:57am
Helios, I don't really understand what you're saying about VM's there, whats your point?
That the difference between "returning an array" and returning an object that wraps the array is of use only to the type system.
May 26, 2011 at 12:58am
Thank you, that's kind of what I was trying to get at in my original post.
May 26, 2011 at 1:12am
Oh. I thought you were nitpicking.
May 26, 2011 at 1:15am
Haha it's okay, I was just trying to make sense of what you had done :)
Topic archived. No new replies allowed.