Returning an array from a function.

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.
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;
}

ok thankyou :)
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];
}
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.
Interesting idea Helios. So you're not really returning an array, but rather an instance of your Array struct?
Last edited on
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.
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
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
What should I find wrong?
The circular reasoning, for one.
See my edit.
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
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.
Thank you, that's kind of what I was trying to get at in my original post.
Oh. I thought you were nitpicking.
Haha it's okay, I was just trying to make sense of what you had done :)
Topic archived. No new replies allowed.