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